0

I am having troubles while trying to post some datas with one file (an image) that I want to upload. Using PHP 5.3.3 and CURL 7.20.0.

Here is the php script (the image is in the same folder I have checked that the path is valid).

function curl_post_request($url, $data, $referer='') {
$data = http_build_query($data); // seems to be required arrays should'nt be supported ? whatever.
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_REFERER, $referer);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($c, CURLOPT_HEADER, $headers); 
curl_setopt($c, CURLINFO_HEADER_OUT, true);
curl_setopt($c, CURLOPT_VERBOSE, true);
$output = curl_exec($c);
var_dump(curl_getinfo($c, CURLINFO_HEADER_OUT));
//var_dump($data);
if($output === false) trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
curl_close($c);
return $output;
}

if(isset($_GET['GO'])) {

$data = array(
'pic1' => "@".realpath('image.jpg'),
'postedvar1' => 'test1',
'postedvar2' => 'test2'
);
$url = 'http://localhost/test/index.php';
$a = curl_post_request($url, $data);
var_dump($a);

} else {

print_r($_POST);
print_r($_FILES);
}

What am I missing? Is this working for you guys?

The curl request seems to be fine, take a look at the following results :

    headers = POST /test/test.php HTTP/1.1
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1
    Host: localhost
    Accept: */*
    Referer: 
    Content-Length: 82
    Content-Type: application/x-www-form-urlencoded

    HTTP/1.1 200 OK
    Date: Sun, 11 Sep 2011 19:46:18 GMT
    Server: Apache/2.2.16 (Win32) PHP/5.3.3
    X-Powered-By: PHP/5.3.3
    Content-Length: 138
    Content-Type: text/html

    $_POST = Array(
    [pic1] => @C:\wamp\www\test\image.jpg
    [postedvar1] => test1
    [postedvar2] => test2
    )
    $_FILES = Array()
3
  • 1
    What errors do you get? Anything from curl_error()? Commented Sep 11, 2011 at 19:43
  • $_POST is OK but $_FILES is empty ! the arobase seems to have no effect on the request. the request seems to go fine without any file ! Commented Sep 11, 2011 at 19:48
  • Please don't nuke your question with "question is solved!" — StackOverflow is intended to be a resource for the future, as well as to help you right now. Commented Sep 11, 2011 at 20:13

1 Answer 1

1

To use the @filepath method of specifying a file to upload, the value for the CURLOPT_POSTFIELDS option must remain as an array.

The first line of your curl_post_request() function turns the array into a urlencoded string.

See the description of CURLOPT_POSTFIELDS in the manual — http://php.net/curl-setopt

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.