cURL is very important tool for PHP programmers, we tends to do so many thing with using this tool. Posting data, imitating a form post and lot more, it's usage is countless. Some time back I came across situation where we have to post files to a 3rd party faxing service. I knew there are many possibilities but I didn't wanted to get into sockets and file stream to get this done, just wanted to post FILE in addition to rest of the post data.
// URL on which we have to post data $url = "http://localhost/tutorials/post_action.php"; // Any other field you might want to catch $post_data['name'] = "khan"; // File you want to upload/post $post_data['file'] = "@c:/logs.log"; // Initialize cURL $ch = curl_init(); // Set URL on which you want to post the Form and/or data curl_setopt($ch, CURLOPT_URL, $url); // Data+Files to be posted curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // Pass TRUE or 1 if you want to wait for and catch the response against the request made curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // For Debug mode; shows up any error encountered during the operation curl_setopt($ch, CURLOPT_VERBOSE, 1); // Execute the request $response = curl_exec($ch); // Just for debug: to see response echo $response;
Above Code is the bare minimum required to post/upload file using CURL. As used in above example, it's not mandatory to use array index as "file". You can use anything required depending upon the requirement.
Posting multiple files is also easy, with the same code you can add multiple file upload using following lines:
$post_data['alian_error_log'] = "@c:/alian_app_error.log"; $post_data['myapp_error_log'] = "@c:/myapp_error.log";
Here @ is the key character, prefixing local file path with @ does all the trick.
curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.
With PHP we can use cURL using libcurl extension for PHP. It comes bundled with most of the WAMP packages and available on almost all managed/web hosting environments.

#1 by Sony AK on March 25, 2011 - 8:48 pm
Quote
how about uploading multiple file but using the same name? in the html form I am using 'datafile[]' for example. how to simulate this situation with curl?
#2 by Dharmavirsinh Jhala on March 31, 2011 - 10:58 pm
Quote
Hi Sony,
Thanks for your question: while trying to reply your comment I have just discovered that to post FILE Array you have to use code similar to below:
$post_data['upload_file_list[0]‘] = “@c:/oscar.jpg”;
$post_data['upload_file_list[1]‘] = “@c:/inception.jpg”;
I hope this helps…! Good luck.
PS: Just take care of those backward slashes added before quotes..!
they are not part of code example.
#3 by Dharmavirsinh Jhala on March 31, 2011 - 11:02 pm
Quote
Hi, Just take care of those backward slashes added before quotes..!
they are not part of code example.
#4 by franziss on March 31, 2011 - 8:38 am
Quote
Hi, I tried your code, but it doesn't work. Can you help me out? Thanks!
I am trying to send a file from a windows vista pc, c:a.jpg
to a linux server http://server.com/processImage.php
So I have set this $post_data['image'] = "@c:/a.jpg"; but it doesn't work.
#5 by Dharmavirsinh Jhala on March 31, 2011 - 10:55 pm
Quote
I just tried it again with same code using JPEG file and found it working properly. You must make sure that file you have specified does exist on that location else it won\’t work at all. Try to print both POST and FILES variables;
print_r($_POST); print_r($_FILES);
When you post file and if $_POST does not get printed, it means that something is wrong with the file you\’re trying to post.
One more thing you must make sure that CURL extension is enabled and there is not file-size limit in place at the receiving end on linux-box.
One question: are you able to post simple text file? Let me know what fixed your issue.
#6 by franziss on April 1, 2011 - 7:25 am
Quote
Hi Jhala,
Thank you for your help. I think this code will only work if the file to be uploaded and the server are in the same PC. I tried uploading a file to the server, which the file resides in the same PC as the server. It works.
But if the file is in other PCs, the server could not locate this file with this command
$post_data['image'] = "@c:/a.jpg";
Because it assumes the path is in the PC of the server.
I put print_r($_POST); print_r($_FILES); echo "Hello";
in processImage.php, but it doesn't show anything. It will show when I remove the '@' infront of c:a.jpg, which it will assume that it is a string
#7 by Dharmavirsinh Jhala on April 1, 2011 - 9:35 pm
Quote
Hello Franziss,
I agree, this code is meant to upload files from the local machine. Means file should be in same machine where your webserver is. I forgot to mention this thought it\’s implicit.
This can be helpful when you have to post something on remote web-server as a part of some cron-job and file already exists on web-server locally.
#8 by Eddwin on February 15, 2012 - 5:54 pm
Quote
i can upload the picture using Curl and get exacly what you get the only thing is that. I dont know how to upload the IMAGE as jpg to the server i only keep uploading a .tmp file. can anyone help me out?
#9 by Dharmavirsinh Jhala on March 7, 2012 - 12:22 am
Quote
You just need to rename that file to it's original name.
I think following link should help you: http://php.net/manual/en/function.move-uploaded-f…
#10 by Nailiz on May 18, 2012 - 5:45 pm
Quote
I was trying to upload image directly to an other site and to make it work properly i finaly find that i have to add the mimetype in the url of the file
"fullpath/img.gif"
To
"fullpath/img.gif;type=image/gif
#11 by Roland Hentschel on July 19, 2012 - 1:48 pm
Quote
Hi!
Since weeks I'm looking for something like this, but with one important difference :
I want to use a form to run the upload, so I need something like
$post_data['file'] = "@",realpath($_FILES["file"]["tmp_name"];
Couldn't find any working sample anywhere …
Help would be appreciated very much !
#12 by Ricardo on July 29, 2012 - 1:38 am
Quote
Thanks!
After a lot of search finally something very well done!
Thanks Again
#13 by mdesign on February 7, 2013 - 9:08 pm
Quote
What if i want a filename like: österreich.log?
$post_data['myapp_error_log'] = "@c:/österreich.log";
and the sender system is windows, but the receiver server is linux.
How can i set the encoding in the FILES array?
Array
(
[name] => .sterreich.log
[type] => text/plain
[tmp_name] => /tmp/phpLXyNDv
[error] => 0
[size] => 120
)
#14 by Dharmavirsinh Jhala on February 7, 2013 - 10:34 pm
Quote
Your output webpage should have following header "content-type: text/html; charset=utf8" and you have to put following line in head section;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
This should serve the problem then rest of the problem needs to be taken care at linux side.
#15 by جيمزات on March 9, 2013 - 9:32 pm
Quote
hi this will be better
function post_files($url,$files) {
//Post 1-n files, each element of $files array assumed to be absolute
// path to a file. $files can be array (multiple) or string (one file).
// Data will be posted in a series of POST vars named $file0, $file1…
// $fileN
$data=array();
if (!is_array($files)) {
//Convert to array
$files[]=$files;
}
$n=sizeof($files);
for ($i=0;$i<$n;$i++) {
$data['file'+$i]="@".$files[$i];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
thank you anyway
#16 by Jawad Ahmad Malik on March 20, 2013 - 3:53 pm
Quote
thnks i worked nicely..:)