PHP   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – PATCH / PUT不接受multipart / form-data文件上传?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
知道为什么PATCH和PUT不接受multipart / form-data文件上传?

当我运行var_dump($_ FILES)时,它输出array(0){}.任何想法为什么会这样?如果我发布文件,它可以正常工作.

下面是我正在运行的请求的示例.

提前致谢!

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__--

解决方法

使用PUT请求上载文件时,不要使用multipart / form-data. PUT请求几乎与GET请求相同.您应该做的就是将文件的内容放在请求的正文中.之后,您可以使用以下代码检索文件,如php文档中所述:

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

大佬总结

以上是大佬教程为你收集整理的php – PATCH / PUT不接受multipart / form-data文件上传?全部内容,希望文章能够帮你解决php – PATCH / PUT不接受multipart / form-data文件上传?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。