Skip to content

Instantly share code, notes, and snippets.

@simonbernard
Last active January 28, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbernard/9001707 to your computer and use it in GitHub Desktop.
Save simonbernard/9001707 to your computer and use it in GitHub Desktop.
Ever tried to upload a file via PHP's cURL? Here is how I did it. I hope it well help someone.
// client side upload
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data")); // i guess it's the default
curl_setopt($ch, CURLOPT_URL, 'http://url/to/upload');
curl_setopt($ch, CURLOPT_POST, true);
$file = curl_file_create('/path/to/your/file.zip');
$post = array('anyParameterYouNeed' => 'anyValue', 'fileArrayIndexOnServer' => $file);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData);
curl_exec($ch);
// and on the server side it looks like this
move_uploaded_file($_FILES['fileArrayIndexOnServer']['tmp_name'], '/upload/path/' . $_FILES['fileArrayIndexOnServer']['name']));
// all additional parameters are in the post array
echo $_POST[anyParameterYouNeed'];
@simonbernard
Copy link
Author

Just found out, that the @filename syntax is deprecated, so the correct way is to use curl_file_create().

@lucnap
Copy link

lucnap commented Jan 28, 2016

Great! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment