how post a file via curl function in php?

i want to write a script which post a form automatically. it is not a spammer! there is a picture field in form. i want to write the script with php and using curl() function. how can i implement file uploading? and is the php suitable for this purpose? i mean form posting?

Asked By: hd.
||

Answer #1:

To upload a file that's on your server, yes, curl can do the trick.

You'll want to use the CURLOPT_POSTFIELDS option, passing it to the curl_setopt function (quoting) :

The full data to post in a HTTP "POST" operation.
To post a file, prepend a filename with @ and use the full path.


Not tested, but I suppose that something like this should work :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com/destination-of-upload.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setpopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../some-file.txt',    // you'll need to adapt this
        // some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);
Answered By: Pascal MARTIN
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles