Dherya Dherya - 1 year ago 66
PHP Question

file post/upload through curl in php

This is my curl initiation code:

$args = new CURLFile('down.png', 'image/png','test'); //file to post
$postFields = array('file' => $args);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
$r = curl_exec($curl);
print_r($r); // to get response from the page

And this is to check whether given file is getting posted or not:




if( $_FILES ) {
var_dump($_FILES );}

I am testing this code on local. Posting fields like string or integer is working fine but posting an image or any other file is not giving me any output. Am I missing some options in
or doing something wrong? I have checked the docs already php docs. Help me out please.

And please comment before downvoting :/ if you have to :(

Answer Source

$target must be "http://[HOST]/url/curlUploadHandler.php"

Check for errors

if( $errno = curl_errno($curl) ) { 
    $error_message = curl_strerror($errno); 
    echo "cURL error ({$errno}):\n {$error_message}"; 


  • put in your code echo getcwd(); to check the full path
  • check with is_readable the image
  • check permisions of image
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download