cURL is a great library. It can do just about anything that a normal web browser can do including send a file via a post request.

This makes it really easy to transmit files between computers. In my case, I was looking for an easy way to send images snapped by various webcam systems to a central server with php managing the images.

Here is a simple script to send a file with php/cURL via POST:


        $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$target_url);
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$result=curl_exec ($ch);
	curl_close ($ch);
	echo $result;

And here is the corresponding script to accept the file.

	if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
	    echo "File is valid, and was successfully uploaded.\n";
	} else {
	    echo "Possible file upload attack!\n";
	echo 'Here is some more debugging info:';
	echo "\n
\n"; print_r($_POST); print "\n"; ?>

And that's it.
Navigate to the 'send' script and it will transmit the file sample.jpeg to the accept script.

Note that you can include other arguments in the post along with the file. This allows you to authenticate the upload. I'm using pre-shared strings to 'validate' that upload came from my send script.

This works with the command line version of php too.


UPDATE Oct. 2014 – 

If you're using PHP 5.5 or better, check out the recently added 'CurlFile' class which makes the whole process a lot easier.




15 responses to “Send a file via POST with cURL and PHP”

  1. makalbo Avatar

    I read your article with great pleasure. Thank you.

  2. David Lenz Avatar

    Thank you so much, this little script worked perfectly! I used the extra post information to add a little extra security, and I need to add IP restriction, but this is a fantastic resource.

  3. Rahul Sharma Avatar

    Really awesome…

    helped a lot…

  4. Waboodoo Avatar

    As it turns out attaching files using the '@'-plus-filename-method is deprecated as of PHP 5.5.0.

    However, here is a helpful article on StackOverflow that solves the problem:

    The main difference is that you have to use the CurlFile class:

    $post = array('file' => new CurlFile($file_name_with_full_path, 'text/plain' /* MIME-Type */, '');

    1. Jason Avatar

      Thanks for the CurlFile addition, fixed my issue!

  5. Good article Avatar
    Good article

    It's  a good article  but I have  small problem with it .. 


    $result=curl_exec ($ch);
    curl_close ($ch);
    echo $result;


    The first line  " curl_exec "  sends the request and print the reply without needing to write echo $result .. 

    In my case , I don't need to echo the resule .. I jsut need to get it in a variable to manipulate it .. 


    How can I do that ? 


    1. derak Avatar

      I should have set ‘CURLOPT_RETURNTRANSFER' to 1 to have curl save the result of the request to $result. I'll update the example.
      Thanks for the feedback.


  6. Dan Avatar

    Great post, thanks a alot.

    I just wanted to point out you can set the MIME type of the posted file by appending the file's path with ';type=mime/type' and you can rename the file so it's posted under a different name by appending ';'.

    So to use the article's code as an example:

    $post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path.';type=image/jpeg;filename=a-differnent-name.jpeg');

  7. Ibrahim Vatih Avatar

    About CURL, i really need help to solve this problem.

    I want to upload video to Facebook from my server with CURL, not <form>. Because Facebook Apps documentation is poor. I was tried some snippets, but fail at all.

    Help me please :/

  8. b_lino Avatar

    Hi, this is a great script. You saved my day.

    Thak you

  9. Nikolay Avatar

    Thanks, man!!! It works! Exactly what I was looking for :)

  10. Doctor Watts Avatar
    Doctor Watts

    Worked first time. Thank you sir!

  11. العاب Avatar

    very nice put is there are file upload limit in curl ? thank you

  12. العاب Avatar


    // ——————- do request ———————+
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, $post_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
             'Content-type: multipart/form-data;'
     ) );

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $response = curl_exec($ch);

  13. jefk Avatar

    @ prefix is deprecated. If you are on php 5.5. you can use CURLFile class or curl_file_create function.

    This libf does it:


Verified by MonsterInsights