Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php ZipArchive check if Zip file is broken/incomplete

Tags:

php

zip

My users upload zip files through FTP, then a php file adds them to a RSS file.

I'm trying to find a way to check each ZIP files to validate the file and check if it is broken or if the upload is unfinished. Is there a way to do that ?

like image 309
Anar Choi Avatar asked Jul 06 '13 03:07

Anar Choi


1 Answers

The result from open can be also be true, which should be evaluated first. Without the check ZipArchive:ER_NOZIP, which equals (int) 1, will always match.

$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);
    }
}
like image 117
h0tw1r3 Avatar answered Nov 15 '22 15:11

h0tw1r3