1

I have a front end php application that is connected to an API. The application talks to the API through PHP curl as that is the only mode of communication.

I need to upload files to the server, these files are taken from the browser and sent to the server.

What would be the best way to send a multi part file to the server using curl.

Currently i am sending the file using $_POST , but i dont think that is recommended.

2
  • Do you have control over the API? If not, does the API spec say anything about the method of uploading stuff? Commented May 10, 2012 at 8:47
  • well, i do have control over the api, but the developer just used the basic POST variable, ofcourse that would need to be changed, but i need to make sure, if it works from the front end Commented May 10, 2012 at 10:21

1 Answer 1

1

If your using curl POST then you can just add the file to the POST with @

// same as <input type="file" name="fileField">
$post = array(
    "fileField"=>"@/path/to/file.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

There are plenty of results on google

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

1 Comment

Just a note that it is necesarry to pass the "filefield" as an array. If it is passed explicitly like "filefield=@path-to_file", it won't work.

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.