Home » Php » curl – Can anyone give me an example for PHP's CURLFile class?

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

Posted by: admin April 23, 2020 Leave a comment


I had a very simple PHP code to upload a file to a remote server; the way I was doing it (as has been suggested here in some other solutions) is to use cUrl to upload the file.

Here’s my code:

$ch = curl_init("http://www.remotesite.com/upload.php"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileupload' => '@'.$_FILES['Filedata']['tmp_name'])); 
echo curl_exec($ch);        

The server is running PHP 5.5.0 and it appears that @filename has been deprecated in PHP >= 5.5.0 as stated here under the CURLOPT_POSTFIELDS description, and therefore, I’m getting this error:

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

Interestingly, there is absolutely nothing about this Class on php.net aside from a basic class overview. No examples, no description of methods or properties. It’s basically blank here. I understand that is a brand new class with little to no documentation and very little real-world use which is why practically nothing relevant is coming up in searches on Google or here on Stackoverflow on this class.

I’m wondering if there’s anyone who has used this CURLFile class and can possibly help me or give me an example as to using it in place of @filename in my code.


I wanted to add my “upload.php” code as well; this code would work with the traditional @filename method but is no longer working with the CURLFile class code:

$folder = "try/";
$path = $folder . basename( $_FILES['file']['tmp_name']); 
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ".  basename( $_FILES['file']['tmp_name']). " has been uploaded";

Final Edit:

Wanted to add Final / Working code for others looking for similar working example of the scarcely-documented CURLFile class …

curl.php (local server)

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="Filedata" id="Filedata" /> 
<br />
<input type="submit" name="submit" value="Submit" />

if ($_POST['submit']) {
    $uploadDir = "/uploads/";
    $RealTitleID = $_FILES['Filedata']['name'];
    $ch = curl_init("http://www.remotesite.com/upload.php"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $args['file'] = new CurlFile($_FILES['Filedata']['tmp_name'],'file/exgpd',$RealTitleID);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 
    $result = curl_exec($ch);       

upload.php (remote server)

$folder = "try/";
$path = $folder . $_FILES['file']['name']; 
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ".  basename( $_FILES['file']['name']). " has been uploaded";
How to&Answers:

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()){
     $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);

     $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


$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;


CURLFile has been explained well above, but for simple one file transfers where you don’t want to send a multipart message (not needed for one file, and some APIs don’t support multipart), then the following works.

$ch = curl_init('https://example.com');

$verbose = fopen('/tmp/curloutput.log', 'w+'); // Not for production, but useful for debugging curl issues.

$filetocurl = fopen(realpath($filename), 'r');

// Input the filetocurl via fopen, because CURLOPT_POSTFIELDS created multipart which some apis do not accept.

// Change the options as needed.

$options = array(
    CURLOPT_POST => true,
        'Content-type: application/whatever_you_need_here',
        'Authorization: Basic ' . $username . ":" . $password) // Use this if you need password login
    CURLOPT_TIMEOUT => 3600,
    CURLOPT_INFILE => $filetocurl,
    CURLOPT_INFILESIZE => filesize($filename),
    CURLOPT_VERBOSE => true,
    CURLOPT_STDERR => $verbose  // Remove this for production

if (curl_setopt_array($ch, $options) !== false) {
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
} else {
    // A Curl option could not be set. Set exception here

Note the above code has some extra debug – remove them once it is working.


Php POST request send multiple files with curl function:

    $file1 = realpath('ads/ads0.jpg');
    $file2 = realpath('ads/ads1.jpg');
    // Old method
    // Single file
    // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file' => '@'.$file1);    
    // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[0]' => '@'.$file1, 'file[1]' => '@'.$file2);
    // CurlFile method   
    $f1 = new CurlFile($file1, mime_content_type($file1), basename($file1)); 
    $f2 = new CurlFile($file2, mime_content_type($file2), basename($file2)); 
    $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[1]' => $f1, 'file[2]' => $f2);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://url.x/upload.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // !!!! required as of PHP 5.6.0 for files !!!
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $res2 = curl_exec($ch);
    echo $res2;

    // upload.php
    $json = json_decode(file_get_contents('php://input'), true);
    if(!empty($json)){ print_r($json); }
    if(!empty($_GET)){ print_r($_GET); }
    if(!empty($_POST)){ print_r($_POST); }
    if(!empty($_FILES)){ print_r($_FILES); }