我的用户通过FTP上传zip文件,然后一个php文件将它们添加到RSS文件中。
我正在尝试找到一种方法来检查每个ZIP文件,以验证文件是否损坏或上传是否未完成。有没有办法实现这个功能?
我正在尝试找到一种方法来检查每个ZIP文件,以验证文件是否损坏或上传是否未完成。有没有办法实现这个功能?
open
得到的结果也可以是true
,应该首先进行评估。如果没有检查ZipArchive:ER_NOZIP
,它相当于(int)1,将始终匹配。< / p >$zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CHECKCONS);
if ($res !== TRUE) {
switch($res) {
case ZipArchive::ER_NOZIP:
die('not a zip archive');
case ZipArchive::ER_INCONS :
die('consistency check failed');
case ZipArchive::ER_CRC :
die('checksum failed');
default:
die('error ' . $res);
}
}
<?php
$maxsize = 1024*1024;
$z = new ZipArchive;
$r = $z->open("foo.zip", ZipArchive::CHECKCONS);
if($r !== TRUE)
die('ZIP error when trying to open "foo.zip": '.$r);
$stat = $z->statName("mybrokenfile.txt");
if($stat['size'] > $maxsize)
die('File too large, decompression denied');
$s = $z->getStream($file);
$data = stream_get_contents($s, $maxsize);
fclose($s);
if($stat['crc'] != crc32($data))
die('File is corrupt!');
//echo 'File is valid';
//you may send the file to the client now if you didn't output anything before
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="mybrokenfile.txt"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $stat['size']);
ob_clean();
echo $data;
$z->close();
?>
ZipArchive
类来实现此功能。自 PHP5.2 以来,它已成为标准的 php 发布版的一部分。按以下方式使用:$zip = new ZipArchive();
// ZipArchive::CHECKCONS will enforce additional consistency checks
$res = $zip->open('test.zip', ZipArchive::CHECKCONS);
if(!$res) {
throw Exception('Error opening zip');
}
switch($res) {
case ZipArchive::ER_NOZIP :
die('not a zip archive');
case ZipArchive::ER_INCONS :
die('consistency check failed');
case ZipArchive::ER_CRC :
die('checksum failed');
// ... check for the other types of errors listed in the manual
}
$zip->open()
会返回ZipArchive::ER_NOZIP
。$zip->open()
将返回 ER_NOZIP
。 - hek2mglswitch(true) { case $res === ZipArchive::ER_NOZIP: ... and so on }
- hek2mgl