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

查看:1483
本文介绍了(CURL,PHP)使用PUT请求上传文件,如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在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 -HX-TOKEN:test123-F filedata = @ / home / weldan /图片/ -Pictures1.jpg http://host.tld/process.php 
Array

[-------------------- ---------- 22031b6e799c
Content-Disposition:_form-data; _name] =>filedata; filename =Cool-Pictures1.jpg
Content-Type:image / jpeg



我知道有上传的文件。



当前的问题是,如何处理此文件,如$ _FILES变量?





感谢

解决方案

$ _FILES数组填充PUT请求。这是因为PUT请求将指定url中的文件名和正文中的文件内容。没有更多。



您已经建议使用 php:// input >

upload.php

 <?php 
/ * PUT数据转到php:// input * /
echo file_get_contents(php:// input);然后使用以下curl命令行:


curl --upload -HX-TOKEN:test123a.txt http://localhost/upload.php

在注释后更新:使用--upload选项应该适合您的需要。不同于-X PUT与-F,数据将发送原始和获取多部分/表单数据编码.. curl的隐藏的宝藏之一);


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

解决方案

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

这篇关于(CURL,PHP)使用PUT请求上传文件,如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆