How upload file using cURL?

Here I am giving an example of how you can upload file using cURL in PHP.

Code:

<?php
	$request_url = ‘http://www.akchauhan.com/test.php’;
        $post_params['name'] = urlencode(’Test User’);
        $post_params['file'] =@.'demo/testfile.txt’;
        $post_params['submit'] = urlencode(’submit’);
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $request_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
        $result = curl_exec($ch);
        curl_close($ch);
?>

Here we have first initialized the new CURL session using “curl_init” function. Then we have set few curl options for CURL session.

CURLOPT_URL: URL to fetch.

CURLOPT_POST: TRUE to regular HTML POST.

CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of “curl_exec” instead of outputting it out directly.

CURLOPT_POSTFIELDS: The full data to post in a HTTP “POST” operation.

You can also set more options as per your requirement but these are the minimum require options for file upload. You can find more information about these options in PHP manual.

Then execute the CURL session through “curl_exec” function. This function will return the output of request.

Here the tricky part is ‘@’ symbol before file name. CURL automatically upload the give file to server or requested URL when it find the ‘@’ symbol in starting of file name.

Note: If you try this code on localhost under windows operating system then you might get the error message “CURL is failed to created the post data”. But this code is work fine under Linux environment. So, try this on your server.

The requested file “test.php” consider this request as simple POST FROM request. Like if you use this form for request.

<form method=”post” action=”test.php” enctype=”multipart/form-data”>
	<input type=textname=namevalue=”Test User” />
	<input type=”file” name=”file” />
	<input type=”submit” name=”submit” value=”submit” />
</form>

, ,


  1. #1 by Movis - May 20th, 2009 at 17:42

    Thanks!

  2. #2 by Ivan - September 30th, 2010 at 10:15

    Nice work mate.
    i’m writing the same article too and other awesome things you can do with cURL that maybe you interest you http://bit.ly/9Okf0x

  3. #3 by Rohan - December 25th, 2010 at 20:18

    Hi Anil,

    As mentioned by you, I tried out the code on Apache PHP localhost and it failed as you said.

    However, when I tried the same with Lighttpd PHP localhost, it worked. Similarly it worked on my production server.

    This is probably an Apache bug.

  4. #4 by Tri Nguyen - October 21st, 2011 at 00:27

    @ Ivan
    Hi Ivan,

    I am starting to learn curl.

    I have no idea where I can start. It would be nice if you can guide me thru.

    I have a new firmware in my local director home/bin/file.dat., now I want to write a curl script to load this file into the machine and run this 20 time to determine howlong it takes from the time it start to load the file until it finish.
    But I have no clues how curl work.
    Please help me

(will not be published)

 


Submit Comment
Subscribe to comments feed