Last active
January 28, 2016 19:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just found out, that the @filename syntax is deprecated, so the correct way is to use curl_file_create().