php .rar, .zip 文件 MIME 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6977544/
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
.rar, .zip files MIME Type
提问by mrdaliri
I'm developing a simple php upload script, and users can upload only ZIP and RAR files.
我正在开发一个简单的 php 上传脚本,用户只能上传 ZIP 和 RAR 文件。
What MIME types I should use to check $_FILES[x][type]
? (a complete list please)
我应该使用哪些 MIME 类型来检查$_FILES[x][type]
?(请提供完整列表)
Thank you..
谢谢..
回答by Gfy
The answers from freedompeace, Kiyarash and Sam Vloeberghs:
自由和平组织、Kiyarash 和 Sam Vloeberghs 的回答:
.rar application/x-rar-compressed, application/octet-stream
.zip application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip
I would do a check on the file name too. Here is how you could check if the file is a RAR or ZIP file. I tested it by creating a quick command line application.
我也会检查文件名。以下是检查文件是 RAR 还是 ZIP 文件的方法。我通过创建一个快速命令行应用程序来测试它。
<?php
if (isRarOrZip($argv[1])) {
echo 'It is probably a RAR or ZIP file.';
} else {
echo 'It is probably not a RAR or ZIP file.';
}
function isRarOrZip($file) {
// get the first 7 bytes
$bytes = file_get_contents($file, FALSE, NULL, 0, 7);
$ext = strtolower(substr($file, - 4));
// RAR magic number: Rar!\x1A\x07\x00
// http://en.wikipedia.org/wiki/RAR
if ($ext == '.rar' and bin2hex($bytes) == '526172211a0700') {
return TRUE;
}
// ZIP magic number: none, though PK$ rar.exe l somefile.zip
somefile.zip is not RAR archive
3$ rar.exe l somefile.srr
SFX Volume somefile.srr
4, PKapplication/zip, application/octet-stream
5application/zip
6 (empty archive),
// or PK $fileInfo = new finfo(FILEINFO_MIME_TYPE);
$fileMime = $fileInfo->file($_FILES['upfile']['tmp_name']);
$validMimes = array(
'zip' => 'application/zip',
'rar' => 'application/x-rar',
);
$fileExt = array_search($fileMime, $validMimes, true);
if($fileExt != 'zip' && $fileExt != 'rar')
throw new RuntimeException('Invalid file format.');
7extension=php_fileinfo.dll
8 (spanned archive) are common.
// http://en.wikipedia.org/wiki/ZIP_(file_format)
if ($ext == '.zip' and substr($bytes, 0, 2) == 'PK') {
return TRUE;
}
return FALSE;
}
Notice that it still won't be 100% certain, but it is probably good enough.
请注意,它仍然不能 100% 确定,但它可能已经足够好了。
import Foundation
import MobileCoreServices
extension URL {
var mimeType: String? {
guard self.pathExtension.count != 0 else {
return nil
}
let pathExtension = self.pathExtension as CFString
if let preferredIdentifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, nil) {
guard let mimeType = UTTypeCopyPreferredTagWithClass(preferredIdentifier.takeRetainedValue(), kUTTagClassMIMEType) else {
return nil
}
return mimeType.takeRetainedValue() as String
}
return nil
}
}
But even WinRAR detects non RAR files as SFX archives:
但即使是 WinRAR 也将非 RAR 文件检测为 SFX 档案:
$allowedExtensions = array( 'mkv', 'mp3', 'flac' );
$temp = explode(".", $_FILES[$file]["name"]);
$extension = strtolower(end($temp));
if( in_array( $extension, $allowedExtensions ) ) { ///
回答by Wilt
For upload:
对于上传:
An official list of mime types can be found at The Internet Assigned Numbers Authority (IANA) . According to their list Content-Type
header for zip
is application/zip
.
可以在Internet Assigned Numbers Authority (IANA) 中找到 mime 类型的官方列表。根据他们的列表Content-Type
标题zip
是application/zip
.
The media type for rar
files is not officially registered at IANA but the unofficial commonly used mime-type value is application/x-rar-compressed
.
rar
文件的媒体类型未在 IANA 正式注册,但非官方常用的 mime-type 值为application/x-rar-compressed
.
application/octet-stream
means as much as: "I send you a file stream and the content of this stream is not specified"(so it is true that it can be a zip
or rar
file as well). The server is supposed to detect what the actual content of the stream is.
application/octet-stream
意味着:“我向您发送了一个文件流,但未指定该流的内容”(因此它也可以是一个zip
或rar
文件)。服务器应该检测流的实际内容是什么。
Note:For upload it is not safe to rely on the mime type set in the Content-Type
header. The header is set on the client and can be set to any random value. Instead you can use the php file infofunctions to detect the file mime-type on the server.
注意:对于上传,依赖Content-Type
标题中设置的 mime 类型是不安全的。标头在客户端设置,可以设置为任何随机值。相反,您可以使用php 文件信息函数来检测服务器上的文件 MIME 类型。
For download:
下载:
If you want to download a zip
file and nothing else you should only set one single Accept
header value. Any additional values set will be used as a fallback in case the server cannot satisfy your in the Accept
header requested mime-type.
如果你想下载一个zip
文件而不是别的,你应该只设置一个Accept
标题值。如果服务器无法满足您在Accept
请求的标头中的 MIME 类型,则设置的任何其他值都将用作后备。
According to the WC3 specificationsthis:
根据WC3规范:
##代码##will be intrepreted as: "I prefer a application/zip
mime-type, but if you cannot deliver this an application/octet-stream
(a file stream) is also fine".
将被解释为:“我更喜欢application/zip
mime 类型,但如果你不能提供这个application/octet-stream
(文件流)也可以”。
So only a single:
所以只有一个:
##代码##Will guarantee you a zip
file (or a 406 - Not Acceptable
response in case the server is unable to satisfy your request).
将向您保证一个zip
文件(或406 - Not Acceptable
在服务器无法满足您的请求时的响应)。
回答by fibriZo raZiel
You should not trust $_FILES['upfile']['mime']
, check MIME type by yourself. For that purpose, you may use fileinfo
extension, enabled by default as of PHP 5.3.0.
你不应该相信$_FILES['upfile']['mime']
,自己检查 MIME 类型。为此,您可以使用fileinfo
extension,自 PHP 5.3.0 起默认启用。
NOTE: Don't forget to enable the extension in your php.ini
and restart your server:
注意:不要忘记启用扩展php.ini
并重新启动服务器:
回答by Wolfgang Schreurs
In a linked question, there's some Objective-C code to get the mime type for a file URL. I've created a Swift extension based on that Objective-C code to get the mime type:
在链接的问题中,有一些 Objective-C 代码可以获取文件 URL 的 MIME 类型。我已经基于该 Objective-C 代码创建了一个 Swift 扩展来获取 mime 类型:
##代码##回答by theking2
As extension might contain more or less that three characters the following will test for an extension regardless of the length of it.
由于扩展可能包含或多或少三个字符,以下将测试扩展,而不管它的长度如何。
Try this:
尝试这个:
##代码##to check for all characters after the last '.'
检查最后一个 '.' 之后的所有字符