How to send a file via curl function in php?

I want to write a script that automatically submits a form. this is not a spammer! there is an image field in the form. I want to write a script with php and use curl () function. how can i implement file upload? and is php suitable for this purpose? i mean form placement?

+2


source to share


1 answer


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

You want to use the option by passing it (quoting): CURLOPT_POSTFIELDS

curl_setopt

Complete data to publish in HTTP "POST" operation.
To publish the file, add the file name with @

and use the full path.




Untested, but I suppose 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);

      

+4


source







All Articles