Author Topic: PHP Curl upload file  (Read 2461 times)

0 Members and 1 Guest are viewing this topic.

Offline jazzTopic starter

  • Irregular
  • Posts: 24
    • View Profile
PHP Curl upload file
« on: July 01, 2009, 04:51:18 PM »
Hi,

I am dead in the water with this. I cannot discover the problem.

I am trying to upload a file with curl and also pass a text value. It refuses to work. :)


Here 's a test form:
http://85.17.135.223/test.php

Code: [Select]
<?php

if (!empty($_REQUEST)) {
var_dump ($_REQUEST);
}

?>


<html>
<body>
<form>
<input type="text" name="a" value="value1" />
<input type="file" name="file1" />
<input type="submit" />
</form>
</body>

And then there 's the php script.

Code: [Select]
$url = 'http://85.17.135.223/test.php';
$file = getcwd().'/'.'step3.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('type' => 'direct', 'file1'=>"@$file", 'a'=>'aaaaa1'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec ($ch);
echo curl_error($ch);
$info = curl_getinfo($ch);
var_dump($info);

Here are the headers:
Quote
POST /test.php HTTP/1.1 Host: 85.17.135.223 Accept: */* Content-Length: 10448 Expect: 100-continue Content-Type: multipart/form-data; boundary=----------------------------b64cdfea8248


It does not work like it does if you do in through the browser. No script output! How can I make this work?


Thank you!

Offline johnathanhebert

  • Irregular
  • Posts: 17
    • View Profile
Re: PHP Curl upload file
« Reply #1 on: July 01, 2009, 05:04:42 PM »
The problem could be related to the "Expect" header that is automatically being added by the curl functions... the server you are posting to may not support it.  Try removing it with:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

http://www.gnegg.ch/2007/02/the-return-of-except-100-continue/

Offline jazzTopic starter

  • Irregular
  • Posts: 24
    • View Profile
Re: PHP Curl upload file
« Reply #2 on: July 01, 2009, 05:17:34 PM »
Thank you for your reply. I added it but to no avail. The Expect header did indeed disapear, but nothing more changed.

Here are the new headers:
Quote
POST /test.php HTTP/1.1 Host: 85.17.135.223 Accept: */* Content-Length: 10495 Content-Type: multipart/form-data; boundary=----------------------------4047ac8569bd