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 CURLOPT_POSTFIELDS
.
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
application/octet-stream
";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 CURLOPT_POSTFIELDS
.
References
Function definition for curl_file_create on PHP < 5.5
Sending files using cURL in PHP (2009)