Page 1 of 1

Posting file with cURL

Posted: Thu Oct 15, 2009 8:20 am
by danc81
Hi,

I'm working on a script that needs to post a file to another script. I'm using curl to do it at the moment which works fine when the file is available locally on the filesystem but I now need to post from an open stream and can't see a way to do it with curl, my current code is:

Code: Select all

 
$formvars = array();
$formvars["field1"] = "xxxxx";
$formvars["field2"] = "xxxxx";
$formvars["imagedata"] = "@$filepath";
 
$ch = curl_init($posturl);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
What I need a way of doing it replacing the "@$filepath" with a stream resource or a stream wrapper path but I can't find a way to do it.

Is there a way to do this with curl? If not, is there an alternative class which could do it? If not, I shall start creating my own.

Thanks.