(CURL, PHP) Uploading files with PUT request, How

2019-02-28 02:16发布

this is what i have in process.php

parse_str(file_get_contents("php://input"),$upload_data);
if (isset($upload_data)) {
   print_r($upload_data);
   exit;
}

this is how i query to server using curl.

weldan@prindu:~$ curl -X PUT -H "X-TOKEN: test123" -F filedata=@/home/weldan/Pictures/Cool-Pictures1.jpg  http://host.tld/process.php
Array
(
    [------------------------------22031b6e799c
Content-Disposition:_form-data;_name] => "filedata"; filename="Cool-Pictures1.jpg"
Content-Type: image/jpeg

���
)

so that how i know there is uploaded file there.

current problem is, how do I process this file like $_FILES variable?

open for other way to achieve this too.

Thanks

1条回答
forever°为你锁心
2楼-- · 2019-02-28 03:07

The $_FILES array being populated on PUT requests. That's because a PUT request will specify the file name in the url and the file content in the body. Nothing more.

You'll have to use php://input as you already suggested.

upload.php

<?php
/* PUT data goes to php://input */
echo file_get_contents("php://input");

Then use the following curl command line:

curl --upload -H "X-TOKEN: test123" a.txt  http://localhost/upload.php

Update after comments: Using the --upload option should fit your needs. In difference to -X PUT together with -F, the data will be send raw and get not multipart/form-data encoded.. One of the hidden treasures of curl ;)

查看更多
登录 后发表回答