Reputation: 2642
I know that we can upload a file with cURL in PHP easily:
$c = curl_init();
curl_setopt($c, CURLOPT_VERBOSE, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_URL, 'http://.....');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, array('file'=>'@/home/myFile.pdf'));
echo curl_exec($c);
I have a linux curl command like this:
curl --location --upload-file /home/myFile.pdf http://...
That works well in a Linux command shell, but in PHP (with the code that I wrote at beginning of my question) I can't upload a file to the url.
I think I must find a way to set "--upload-file" option of the cURL command in PHP.
How can I do this?
Upvotes: 2
Views: 1175
Reputation: 7433
First, change the method to PUT:
// curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_PUT, true);
Then change POSTFIELDS to INFILE:
// curl_setopt($c, CURLOPT_POSTFIELDS, array('file'=>'@/home/myFile.pdf'));
$fp = fopen("/home/myFile.pdf", "rb");
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize('/home/myFile.pdf'));
Upvotes: 3