php 为什么我将 .csv 文件的 mime 类型作为“应用程序/八位字节流”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12061030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting mime-type of .csv file as "application/octet-stream"?
提问by Mohammad Saberi
I'm working on a PHP application that must import an excel file into MySQL. So I need to convert the excel file to .csv format. But when I want to get its type using $_FILE['something']['type'], I get application/octet-streamas its mime-type;
I think there is something wrong here. Because I gathered the list below as a .csv file mime-type:
我正在开发一个必须将 excel 文件导入 MySQL 的 PHP 应用程序。所以我需要将 excel 文件转换为 .csv 格式。但是当我想使用 获得它的类型时$_FILE['something']['type'],我得到application/octet-stream它的 mime-type;
我认为这里有问题。因为我将下面的列表收集为 .csv 文件 MIME 类型:
text/comma-separated-values,
text/csv,
application/csv,
application/excel,
application/vnd.ms-excel,
application/vnd.msexcel
What's the matter ?
怎么了 ?
回答by rdlowrey
In times like these, the official HTTP specification is always helpful. From RFC 2616 7.2.1(my emphasis added):
在这种情况下,官方 HTTP 规范总是很有帮助。从RFC 2616 7.2.1(我的重点补充):
Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".
任何包含实体主体的 HTTP/1.1 消息都应该包含一个 Content-Type 头字段,用于定义该主体的媒体类型。当且仅当媒体类型不是由 Content-Type 字段给出时,接收者可以尝试通过检查其内容和/或用于标识资源的 URI 的扩展名来猜测媒体类型。如果媒体类型仍然未知,则接收者应该将其视为类型 "application/octet-stream"。
The cause of your issue is that the server accepting the file upload does not itself know what type of file has been uploaded. Why? Because it relies on the the HTTP message which sent the file to specify a Content-Typeheader to determine the exact mime-type. The browser has likely not sent a Content-Typeheader and the server has assumed application/octet-streamas per the official HTTP specification excerpt above. It's also possible that the client uploading the file opted not to determine the mime type of the file it was uploading and sent the Content-Type: application/octet-streamheader itself.
您的问题的原因是接受文件上传的服务器本身并不知道上传的是什么类型的文件。为什么?因为它依赖于发送文件的 HTTP 消息来指定一个Content-Type标头来确定确切的 MIME 类型。浏览器可能没有发送Content-Type标头,服务器已application/octet-stream根据上面的官方 HTTP 规范摘录进行假设。上传文件的客户端也有可能选择不确定正在上传的文件的 MIME 类型并Content-Type: application/octet-stream自行发送标头。
Now, when we consider this in conjunction with the PHP manual entry regarding POST file uploadsdocs, we see the following:
现在,当我们结合有关 POST 文件上传文档的PHP 手册条目考虑这一点时,我们会看到以下内容:
$_FILES['userfile']['type']The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
$_FILES['userfile']['type']文件的 MIME 类型(如果浏览器提供了此信息)。一个例子是“图像/gif”。然而,这种 mime 类型不会在 PHP 端检查,因此不要认为它的值是理所当然的。
So as you can see, even if $_FILES['userfile']['type']is specified, it only corresponds to the Content-Typeheader sent by the client. This information can easily be faked and should not be relied upon. If you need to be surethat the uploaded file is of a specific type, you'll have to verify that yourself.
所以可以看到,即使$_FILES['userfile']['type']指定了,也只对应Content-Type客户端发送的header。这些信息很容易被伪造,不应依赖。如果您需要确保上传的文件属于特定类型,则必须自己进行验证。
回答by StrubT
application/octet-streamis always used if the mime type is not known.
application/octet-stream如果 MIME 类型未知,则始终使用。
回答by chucksmash
All of the mime-types you listed show up as common mime-types for csv files on http://filext.com/file-extension/CSV
您列出的所有 mime 类型都显示为http://fileext.com/file-extension/CSV上 csv 文件的常见 mime 类型
So basically I'd say it comes down to which program generated the .csv file and which mime-type they decided to use.
所以基本上我会说这归结为哪个程序生成了 .csv 文件以及他们决定使用哪种 mime 类型。
回答by dev-null-dweller
$_FILE['something']['type']is populated by the browser / user OS, so it is not reliable. You should make your own check at server side to determine if uploaded file was in desired format.
$_FILE['something']['type']由浏览器/用户操作系统填充,因此不可靠。您应该在服务器端自行检查以确定上传的文件是否为所需格式。

