简体   繁体   中英

Fileupload with curl. Php

I use curl in my project. Version of the libcurl is 7.18.2. It's a bug (does't work filetype), more - here . For fileupload I use this code:

if ( !empty($_POST) ){
     $post = $_POST;
 }

if ( !empty($_FILES) ){
   foreach ($_FILES as $param => $file) {
         if ( $file['tmp_name'] )
                $files[$param] = '@' . $file['tmp_name'] . ';filename=' . $file['name'] . ';type=' . $file['type'];
   }
}

$post = !empty($files) ? array_merge($post, $files) : $post;

// ...

if ( $curl = curl_init() ) {

  curl_setopt($curl, CURLOPT_URL, $link);
  curl_setopt($curl, CURLOPT_USERAGENT, $browser);
  curl_setopt($curl, CURLOPT_REFERER, $reffer);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($curl, CURLOPT_COOKIE, $cookie);

  if ( !empty($post) ) {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  }

  $content = curl_exec($curl);

  curl_close ($curl);
}

Please tell me, how I can send type of a file on a server? The libcurl upgrade is not possible. Thanks a lot.

Thanks. I thought about it. In the issue:

curl part:

if ( !empty($_FILES) ){

        foreach ($_FILES as $param => $file) {

            if ( $file['tmp_name'] ){

                $post[$param]='@' . $file['tmp_name'];
                $post['_ftype']=$file['type'];
                $post['_ffilename']=$file['name'];
                $post['_ffileuploadname']=$param;
            }
        }
}

server part:

if ( !empty($_FILES) && isset($_POST['_ffileuploadname']) ){

    $name=$_POST['_ffileuploadname'];
    $_FILES[$name]['type']=$_POST['_ftype'];
    $_FILES[$name]['name']=$_POST['_ffilename'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM