Can anyone give me an example for PHP's CURLFile class? Can anyone give me an example for PHP's CURLFile class? curl curl

Can anyone give me an example for PHP's CURLFile class?


There is a snippet on the RFC for the code: https://wiki.php.net/rfc/curl-file-upload

curl_setopt($curl_handle, CURLOPT_POST, 1);$args['file'] = new CurlFile('filename.png', 'image/png', 'filename.png');curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);

You can also use the seemingly pointless function curl_file_create( string $filename [, string $mimetype [, string $postname ]] ) if you have a phobia of creating objects.

curl_setopt($curl_handle, CURLOPT_POST, 1);$args['file'] = curl_file_create('filename.png', 'image/png', 'filename.png');curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);


Thanks for your help, using your working code I was able to solve my problem with php 5.5 and Facebook SDK. I was getting this error from code in the sdk class.

I don't thinks this count as a response, but I'm sure there are people searching for this error like me related to facebook SDK and php 5.5

In case someone has the same problem, the solution for me was to change a little code from base_facebook.php to use the CurlFile Class instead of the @filename.

Since I'm calling the sdk from several places, I've just modified a few lines of the sdk:

In the method called "makeRequest" I made this change:

In this part of the code:

if ($this->getFileUploadSupport()){    $opts[CURLOPT_POSTFIELDS] = $params;} else {    $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');}

Change the first part (with file upload enabled) to:

if ($this->getFileUploadSupport()){  if(!empty($params['source'])){     $nameArr = explode('/', $params['source']);     $name    = $nameArr[count($nameArr)-1];      $source  = str_replace('@', '', $params['source']);     $size    = getimagesize($source);      $mime    = $size['mime'];     $params['source'] = new CurlFile($source,$mime,$name);  }  if(!empty($params['image'])){     $nameArr = explode('/', $params['image']);     $name    = $nameArr[count($nameArr)-1];      $image   = str_replace('@', '', $params['image']);     $size    = getimagesize($image);      $mime    = $size['mime'];     $params['image'] = new CurlFile($image,$mime,$name);  }  $opts[CURLOPT_POSTFIELDS] = $params;} else {    $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');}

Maybe this can be improved parsing every $param and looking for '@' in the value.. but I did it just for source and image because was what I needed.


FOR curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please usethe CURLFile class instead

$img='image.jpg';$data_array = array(        'board' => $board_id,        'note' => $note,        'image' => new CurlFile($img)    );$curinit = curl_init($url);     curl_setopt($curinit, CURLOPT_RETURNTRANSFER, true);     curl_setopt($curinit, CURLOPT_POST, true);     curl_setopt($curinit, CURLOPT_CUSTOMREQUEST, "POST");     curl_setopt($curinit, CURLOPT_POSTFIELDS, $data_array);     curl_setopt($curinit, CURLOPT_SAFE_UPLOAD, false);     $json = curl_exec($curinit);     $phpObj = json_decode($json, TRUE);       return $phpObj;