简体   繁体   中英

How to upload a file with PHP curl using HTTP PUT?

How can I upload a file with PHP curl, using HTTP PUT instead of POST?

Use case

Assuming the following sample data...

$data = [
  'foo' = bar;
  'image_file' = curl_file_create('C:\sample.jpg','image/jpg','receipt.jpg')
];

I can post and upload the sample data above with the following curl options...

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

This results in the following data on the receiving end of the post...

Array
(
    [foo] => 'bar'
    [image_file] => Array
        (
            [name] => sample.jpg
            [type] => image/jpg
            [tmp_name] => C:\tmp\php8934.tmp
            [error] => 0
            [size] => 351836
        )
)

Now, I'd like to do the same thing using HTTP PUT.

Attempt 1

If change to an http put using the following option...

//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 

...nothing is posted.

Attempt 2

If I also change the postfields option to...

//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

The post works, but the upload doesn't. Here's the result...

Array
(
    [foo] => 'bar'
    [image_file] => Array
        (
            [name] => C:\sample.jpg
            [mime] => image/jpg
            [postname] => sample.jpg
        )
)

Conclusion

I see no way of uploading files using HTTP PUT. Can it be done?

$image = fopen($file_path, "rb");

$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_path));

$result = curl_exec($curl);
curl_close($curl); 

It can be done using code below:

#Initiate cURL object
$curl = curl_init();
#Set your URL
curl_setopt($curl, CURLOPT_URL, 'https://local.simbiat.ru');
#Indicate, that you plan to upload a file
curl_setopt($curl, CURLOPT_UPLOAD, true);
#Indicate your protocol
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
#Set flags for transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
#Disable header (optional)
curl_setopt($curl, CURLOPT_HEADER, false);
#Set HTTP method to PUT
curl_setopt($curl, CURLOPT_PUT, 1);
#Indicate the file you want to upload
curl_setopt($curl, CURLOPT_INFILE, fopen('path_to_file', 'rb'));
#Indicate the size of the file (it does not look like this is mandatory, though)
curl_setopt($curl, CURLOPT_INFILESIZE, filesize('path_to_file'));
#Only use below option on TEST environment if you have a self-signed certificate!!! On production this can cause security issues
#curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
#Execute
curl_exec($curl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM