(CURL, PHP) Uploading files with PUT request, How do I process this?

只谈情不闲聊 提交于 2019-12-02 07:09:10

问题


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:


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 ;)



来源:https://stackoverflow.com/questions/15106849/curl-php-uploading-files-with-put-request-how-do-i-process-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!