PHP 使用 PUT 而不是 POST 上传文件
我在 PHP 文档 上读到了一些相关内容,但我不清楚:
最广泛使用的浏览器(IE、FF、Chrome、Safari、Opera...)是否支持这种
PUT
方法来上传文件?我应该编写什么 HTML 才能使浏览器通过
PUT
请求调用服务器?我的意思是我是否需要编写一个带有INPUT
文件字段的FORM
并将属性method="POST"
替换为method ="PUT"
?在 PHP 文档(上面的链接)上,他们说上传文件时
PUT
请求比POST
请求简单得多,除了这个优点之外,还有什么其他优点/与POST
相比,PUT
有何缺点?
I read something about this on PHP docs, but it's not clear to me:
Do the most widely used browsers (IE, FF, Chrome, Safari, Opera, ...) support this
PUT
method to upload files?What HTML should I write to make the browser call the server via a
PUT
request? I mean do I need to write aFORM
with anINPUT
file field and just replace the attributemethod="POST"
withmethod="PUT"
?On the PHP docs (link above) they say a
PUT
request is much simpler than aPOST
request when uploading file, along with this advantage, what other advantages/disadvanatges do thePUT
has got compared to thePOST
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不能从
http://old.mnot.net/javascript/xmlhttprequest/
正如简短的 Google 搜索所表明的那样,IE 也可能起作用。 Firefox 看起来不错。未检查 Chrome 或 Webkit。
服务器站点需要专门指定的脚本来处理传入的 PUT 请求。查看 Apache 文档。 mod_rewrite 规则通常可以做到。
PUT 的一般优点是不需要将文件编码/编组为 multipart/* mime 类型。理论上,这可以更可靠地上传更大的文件。尽管如果您使用 PHP,它对您没有多大帮助。它适用于具有 WebDAV 支持和/或直接文件系统写入访问权限的 Web 服务器。 (Apache 可以自行保存上传的文件,如果您使用它的话。)
The PUT method cannot be used from a <form>. MSIE does not support it through the user GUI at all.
You can however use XMLHttpRequest. It seems to be defined in the standard and WHATWG / HTML5. My browser (Opera) obviously likes it.
http://old.mnot.net/javascript/xmlhttprequest/
IE might work too, as a short Google search suggests. And Firefox looks fine. Not checked Chrome or Webkit.
Server-site you need a specially designated script to handle an incoming PUT request. Look into the Apache docs. A mod_rewrite rule might usually do.
The genral adavantage of PUT is that there is no file encoding / marshalling into a multipart/* mime type required. In theory this allows uploading larger files more reliably. Allthough if you use PHP, it won't help you much. It's meant for Webservers with WebDAV support and/or direct filesystem write access. (Apache can save uploaded files itself, if you use that.)
我认为大多数主要浏览器都支持该方法,但您无法考虑到每个浏览器和其他客户端。粗略地看一下用户贡献的注释,有时甚至需要服务器端配置才能工作。
此外,处理您可能想要与文件一起发送的任何其他表单值也变得更加困难。
我不会用它。可能带来的麻烦太多,实际收益却很少。
I think the method is supported by most major browsers, but you can't account for every browser and other client that is out there. From a cursory look at the user contributed notes, this sometimes even needs server-side configuration to work.
Also, handling any additional form values you may want to send along with your file becomes more difficult.
I wouldn't use it. Way too much possible hassle for little actual gain.
PUT
很少用于此目的,并且仅受主要浏览器支持,这一事实将其排除在此处的任何可能用途之外。The fact that
PUT
is rarely used for the purpose and only supported by major browsers excludes it from the any possible use here.PUT
并未得到浏览器的广泛支持,并且通常不用于交互式 HTML 表单。PUT
is not very widely supported by browsers, and isn't generally used for interactive HTML forms.