Posting file with cURL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danc81
Forum Newbie
Posts: 16
Joined: Mon May 11, 2009 2:00 pm

Posting file with cURL

Post 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.
Post Reply