Send Files using cURL in Php 5.4
Learn how to send files using cURL in PHP 5.4 and an example code for that.
Since php 5.5, sending files in php has improved a lot. You can use CURLFile class or curl_file_create() to upload a file with
But for older versions, it’s a struggle.
First of all, you can not send files using the GET method. That doesn’t even make sense. Also, you need the absolute path of the file. Relative paths won’t work.
Here’s a sample code that will work-
Above code has 3 parts-
"@" . $fileAbsolutePath
This gives the cURL library full path to the file so it knows which file to send.
";type=" . mime_content_type($fileAbsolutePath)
This is used to set a MIME Content-type for the uploaded file. Without it, MIME type defaults to
";filename=" . basename($fileAbsolutePath)
This is used to give the uploaded file a new name. Use this to change the name of the file that is received by the server on which request is sent.
Now you can use the
$cfile variable to send the file by setting it as a parameter to
Function definition for curl_file_create on PHP < 5.5
Sending files using cURL in PHP (2009)