Ответы от liberpeace, 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-файлом. Я тестировал его, создавая приложение быстрой командной строки.
<?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\003\004, PK\005\006 (empty archive),
// or PK\007\008 (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.zip
somefile.zip is not RAR archive
Но даже WinRAR обнаруживает файлы без RAR как архивы SFX:
$ rar.exe l somefile.srr
SFX Volume somefile.srr