来自freedompeace,Kiyarash和Sam Vloeberghs的答案:
.rar application/x-rar-compressed, application/octet-stream.zip application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip
我也会检查文件名。这是检查文件是RAR还是ZIP文件的方法。我通过创建一个快速的命令行应用程序对其进行了测试。
<?phpif (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!x1Ax07x00 // http://en.wikipedia.org/wiki/RAR if ($ext == '.rar' and bin2hex($bytes) == '526172211a0700') { return TRUE; } // ZIP magic number: none, though PK 03 04, PK 05 06 (empty archive), // or PK 07 08 (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;}请注意,它仍然不能100%确定,但是可能已经足够好了。
$ rar.exe l somefile.zipsomefile.zip is not RAR archive
但是,即使WinRAR也会将非RAR文件检测为SFX存档:
$ rar.exe l somefile.srrSFX Volume somefile.srr



