cURL Sending File as Post


Posted on 16th Feb 2014 07:03 pm by admin

This is a bit complicated, I'm just hoping someone else has run into this and knows how to do it a different way or make it work.

When sending a file as part of form information included in a cURL request, it appears you can not include a query string in the file name. Take a look at my example to see what I'm talking about.

Code: //This creates an example image with some text from $_GET['img'] you can try calling this directly to see what the output looks like
if(isset($_GET['img'])){
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 3, 3, 3, $_GET['img'], $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
exit;
}

//Show what was sent to the $_POST and $_FILES variables
if(isset($_POST['showpost'])){
echo '<pre>';
print_r($_POST);
print_r($_FILES);
exit;
}

//Here's the part where we get in trouble...
//This works
//$imgURL = '@'.$_SERVER['SCRIPT_FILENAME'];

//This doesn't work
$imgURL = '@'.$_SERVER['SCRIPT_FILENAME'].'?img=true';

$data = array('showpost' => 'true', 'file' => $imgURL);

//Now do the cURL request to show information about the file that was sent
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
if(curl_errno($ch))
echo curl_error($ch);
curl_close($ch);
When using the section that says "This doesn't work" you'll get the cURL error "failed creating formpost data".

Any suggestions?

No comments posted yet

Your Answer:

Login to answer
58 Like 39 Dislike
Previous forums Next forums
Other forums

check if value exists
I have googled this for a while and I am getting lots of different results. Is there a standard meth

Slashes
Have a small problem and I'm not able to understand why I'm getting the results I'm getting... and i

Remove directories not empty
hi,
i have an array that contains some names (1,4,6,56...). Every integer is a name of a folder.<

PHP Script runs on CLI but not through web browser
I am running into an issue that I just can't seem to find the answer to. I have a Windows Server 200

classic dynpro : hide area and auto adjust hights
Hi,

I want to develop a similar user interface as is used in standard transaction MIGO.

Need help adding a timestamp to my filename/variable
Hello,

I'm kind of stupid when it comes to php and I need a tiny bit of help. I've got a f

Month String to Numeric?
Hi guys,

Given a month as a string, is there a simple way to find the numeric representation

help with variable
I've got a problem, i want to echo some images depending on the $id, however for me to get that id i

How could I do this?
Ive got a simple lottery game, and am using an sql database to keep track of the numbers bought. But

HELP with mysql_real_escape_string
I'm looking to remove apostrophes completely when someone enters lyrics but don't have a clue how to

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash