(CURL,PHP)使用PUT请求上传文件,我该如何处理?((CURL, PHP) Uploading files with PUT request, How do I process this?)

编程入门 行业动态 更新时间:2024-10-09 22:18:16
(CURL,PHP)使用PUT请求上传文件,我该如何处理?((CURL, PHP) Uploading files with PUT request, How do I process this?)

这就是我在process.php中所拥有的

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

这是我使用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 ��� )

所以我怎么知道那里有上传的文件。

目前的问题是,如何像$ _FILES变量一样处理这个文件?

打开其他方式来实现这一目标。

谢谢

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

最满意答案

在PUT请求中填充$ _FILES数组。 那是因为PUT请求将指定url中的文件名和正文中的文件内容。 而已。

你必须按照你的建议使用php://input 。

upload.php的

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

然后使用以下curl命令行:

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

注释后更新 :使用--upload选项应该符合您的需求。 与-X PUT和-F不同,数据将被原始发送并且不会被多部分/表格数据编码。卷曲的隐藏宝藏之一;)

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

更多推荐

本文发布于:2023-07-04 09:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1022356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我该   如何处理   上传文件   PUT   CURL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!