Table of Contents
Check zip magic number, rar magic number dùng để kiểm tra xem file upload có phải là file *.zip hoặc file *.rar hay không.
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;
}
Nguồn: https://stackoverflow.com/questions/6977544/rar-zip-files-mime-typeKiểm tra phải file upload lên là file zip hay không, nếu đúng thì giải nén ra
if(isRarOrZip($targetFile)){
$zip = new ZipArchive;
$res = $zip->open($targetFile);
if ($res === TRUE) {
$zip->extractTo($foldername.'/'.$namefile);
$zip->close();
echo 'done!';
} else {
echo 'error!';
}
unlink($targetFile);
$dirapp=$diroot.$webfol.'uploads/';
$prefixname='redrose'.'-'.$namefile.'_'.date("dmYgisa");
$fullpathzip=$dirapp.$namefile;
$dirname=$dirapp.$prefixname;
rename($fullpathzip,$dirname);
}
-soiqualang_chentreu
]]>
0 thoughts on “Check zip magic number, rar magic number”