PATCH / PUT不接受multipart / form-data文件上传? [英] PATCH/PUT not accepting multipart/form-data file uploads?

查看:1159
本文介绍了PATCH / PUT不接受multipart / form-data文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在运行 var_dump($ _ FILES)时,如果PATCH和PUT不能接受multipart / form-data文件的上传, )它输出数组(0){} 。任何想法为什么发生这种情况?如果我POST文件,它工作正常。



以下是我正在运行的请求的一个示例。



提前致谢!

  PUT /test.php HTTP / 1.1 
Content-Type:multipart / form-data;边界= __ X_PAW_BOUNDARY__
主机:[删除]
连接:关闭
的User-Agent:爪子/ 2.1.1(麦金塔; OS X / 10.10.2)GCDHTTPRequest
的Content-Length :17961

--__ X_PAW_BOUNDARY__
Content-Disposition:form-data; NAME = 化身;文件名=default.png
内容类型:image / png

PNG


[图像数据]
--__ X_PAW_BOUNDARY__ -


解决方案

使用PUT请求上传文件时,不使用多部分/表单数据。 PUT请求几乎与GET请求相同。所有你应该做的是把文件的内容在请求的主体。之后,您可以使用以下代码检索文件,如php文档中所述:

http://php.net/manual/en/features.file-upload.put-method.php ):

 <?php 
/ * PUT数据来自stdin流* /
$ putdata = fopen( php://输入,r);
$ b $ *打开文件写入* /
$ fp = fopen(myputfile.ext,w); ($ data = fread($ putdata,1024))

$ b $ *每次读取数据1 KB
并写入文件* /
fwrite($ fp,$ data);

/ *关闭流* /
fclose($ fp);
fclose($ putdata);
?>


Any idea why PATCH and PUT wouldn't be accepting multipart/form-data file uploads?

When I run var_dump($_FILES) it outputs array(0) { }. Any ideas why this is happening? If I POST the file, it works fine.

Below is an example of the request I am running.

Thanks in advance!

PUT /test.php HTTP/1.1
Content-Type: multipart/form-data; boundary=__X_PAW_BOUNDARY__
Host: [redacted]
Connection: close
User-Agent: Paw/2.1.1 (Macintosh; OS X/10.10.2) GCDHTTPRequest
Content-Length: 17961

--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="avatar"; filename="default.png"
Content-Type: image/png

PNG


[IMAGE DATA]
--__X_PAW_BOUNDARY__--

解决方案

When uploading files using a PUT request you don't use multipart/form-data. A PUT request is almost the same as a GET request. All you should be doing is putting the contents of the file in the body of the request. After that you can retrieve the file with the following code as explained in the php docs:

http://php.net/manual/en/features.file-upload.put-method.php):

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time
   and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>

这篇关于PATCH / PUT不接受multipart / form-data文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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