0

I'm trying to upload a file on a form on my site, and then pass it on to a remote API.

This is my PHP:

$fields = array(
    'file'=>$_FILES["mediaupload"],
    'username'=>urlencode($_POST["username"]),
    'password'=>urlencode($_POST["password"]),
    'latitude'=>urlencode($_POST["latitude"]),
    'longitude'=>urlencode($_POST["longitude"]),
);
$fields_string = http_build_query($fields);
$url = my_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);

At the moment I keep getting error messages that the file could not be processed properly. The API expects all fields as POST strings except the file, which it expects in binary.

I know it's going to be tough to debug this for you guys without access to the remote API, but am I doing anything obviously wrong, or should this work?

Many thanks.

2
  • does your API have a documentation? they should tell you how to use cURL with it Commented Jun 21, 2011 at 16:00
  • Not sure, but is there a setting in php.ini to enable curl? Check phpinfo() to see if it's enabled or not. Commented Jun 21, 2011 at 16:22

1 Answer 1

5

File upload using curl does not work like this. You need to first save the file locally using php's move_uploaded_file then get the path to file. In the fields add this,

$fields = array(
    'file'=>"@/path/to/myfile.ext",
    'username'=>urlencode($_POST["username"]),
    'password'=>urlencode($_POST["password"]),
    'latitude'=>urlencode($_POST["latitude"]),
    'longitude'=>urlencode($_POST["longitude"]),
);

Also, I'm not sure if fields array needs to be converted to string to be used as postfields. According to manual it can be an array() directly.

CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work.

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

2 Comments

To make your example work with the OP's situation, you'd need 'file' => '@' . $_FILES['mediaupload']['tmp_name'], to point CURL at the temporary file where the upload was place on his server.
Very helpful, i was just going to post a similar question. So can I use the temporary path and specify a mime-type at the same time (as in 'file' => '@'.$_FILES['mediaupload']['tmp_name'].';type='.$_FILES['mediaupload']['type']; ?

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.