简体   繁体   English

(CURL, PHP) 使用 PUT 请求上传文件,我该如何处理?

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

this is what i have in process.php这就是我在 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.这就是我使用 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?当前的问题是,如何像 $_FILES 变量一样处理这个文件?

open for other way to achieve this too.也可以通过其他方式实现这一目标。

Thanks谢谢

The $_FILES array being populated on PUT requests.在 PUT 请求上填充 $_FILES 数组。 That's because a PUT request will specify the file name in the url and the file content in the body.这是因为 PUT 请求将在 url 中指定文件名,在正文中指定文件内容。 Nothing more.而已。

You'll have to use php://input as you already suggested.您必须按照您的建议使用php://input

upload.php上传.php

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

Then use the following curl command line:然后使用以下 curl 命令行:

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

Update after comments : Using the --upload option should fit your needs.评论后更新:使用 --upload 选项应该适合您的需要。 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 ;)与 -X PUT 和 -F 的不同之处在于,数据将被原始发送并且不会被多部分/表单数据编码.. curl 的隐藏宝藏之一;)

Short answer简答

The only way in your situation is to parse raw request manually (look at this gist )在您的情况下,唯一的方法是手动解析原始请求(看看这个 gist

Thoughts想法

  • PUT request could be used to upload one file only (not from html upload form, but using custom request). PUT 请求只能用于上传一个文件(不是来自 html 上传表单,而是使用自定义请求)。 On the server you just get raw file content and writes it into some file.在服务器上,您只需获取原始文件内容并将其写入某个文件。
  • Use PUT without files (application/x-www-form-urlencoded) and parse raw request using parse_str(file_get_contents("php://input"), $_PUT);使用没有文件的 PUT (application/x-www-form-urlencoded) 并使用parse_str(file_get_contents("php://input"), $_PUT);解析原始请求parse_str(file_get_contents("php://input"), $_PUT);
  • Use POST to upload files (multipart/form-data).使用 POST 上传文件(multipart/form-data)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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