« Return to all random blog posts
Jan 2008
Uploading a file using Curl in PHP
Here's how to upload files using curl in php: (it's very easy)
notice the @ infront of the file path, this is the magic part.
<?php
$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, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
Comments
-
Thank you!
-
Immensely useful information. Not found in the PHP manual, not found in the net. Probably somwhere out there, but you are the first to come from search results with what I was looking for: upload of multiple files with a single CURL call in PHP. Thank you very much man!
-
Thank you for this beautiful code...
-
Thanks, I'll try it later. Just needed to know if it's possible.
-
please post an example of how to upload two files simultaneously, i.e. the form on the other side has two browse buttons and when you click submit both files get downloaded simultaneously.
-
Just put:
$post = array(
"file1"=>"@/path/to/myfile1.jpg",
"file2"=>"@/path/to/myfile2.jpg",
);
-
-
Thank you very much from Colombia
-
Very nice, thanks for this post!
-
Thanks for this simple and efficient code!
Just one additinal question: why is there a "@" sign in fromt of the file name?
-
It's required to upload the file
-




Comments: