ZipArchive无法打开文件 - 错误代码:19

时间:2014-12-16 11:27:02

标签: php ziparchive

我的代码能够打开我已经上传并移动到文件夹中的zip文件的问题,zip文件上传很好,你可以在任何Zip程序中打开它然而,当我尝试用ZipArchive打开它时提取错误的数据。

    $path = "../"; // Upload directory      
    $count = 0;

    foreach ($_FILES['files']['name'] as $f => $name) {     
       if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $name))
             $count++; // Number of successfully uploaded file
    }

    $kioskFile = $_FILES['files']['name'][0];
    $kioskFile = explode(".", $kioskFile);
    $kioskFile = $kioskFile[0];
    $zipFile = "../" . $kioskFile . ".zip";     

    $zip = new ZipArchive;
    $res = $zip->open($zipFile);
    if ($res === true) {
        $zip->extractTo("./");
        $zip->close();
    } else {
        echo "Error Cannot Open Zip File - Error Code: ";
    }

当我运行代码时,它会显示错误代码19

ZIPARCHIVE :: ER_NOZIP - 19

这是我得到的错误,但文件存在,如果我使用zip_open,它返回它可以打开zip文件。

任何帮助都会非常棒

EDIT1 如果我上传我手动创建的zip文件(使用zip程序),那么上传工作正常。但是,如果我使用ZipArchive创建的zip文件,那么它会立即出错并出现错误19。

EDIT2 我现在添加了一个检查,以确保该文件存在于正确的目录中,并且还放置了该位置的打印,两者都匹配,但仍然是相同的问题。错误19

    $path = "../"; // Upload directory      
    $count = 0;

    foreach ($_FILES['files']['name'] as $f => $name) {     
       if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $name))
             $count++; // Number of successfully uploaded file
    }

    $kioskFile = $_FILES['files']['name'][0];
    $kioskFile = explode(".", $kioskFile);
    $kioskFile = $kioskFile[0];
    $realpath = realpath("../");
    $zipFile = $realpath . "\\" .  $kioskFile . ".zip";     

    if (file_exists($zipFile)) {
        $extract = zip_extract($zipFile, "../");
        if ($extract === TRUE) {

        } else {
            echo "The file " . $zipFile . " cannot be opened";  
        }
    } else {
        echo "The file " . $zipFile . " does not exist";
        die();
    }

UPDATE1 所以我想我已经把它缩小到这个代码,或者我用来从系统下载.zip文件的下载脚本,如果我在系统上保留zip并使用完全相同的代码,它工作正常

这是我的下载代码,也许我错过了导致此问题的内容。

    $fileID = $_GET['id'];
    $backupLoc = "backups/";
    $sql = "SELECT * FROM backups WHERE id = '" . addslashes($fileID) .  "' LIMIT 1";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res); 
    $backupFile = $row['backupFile'];
    $zipFile = $backupLoc . "/" . $backupFile . ".zip";
    $zipSize = filesize($zipFile);

    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="' . basename($zipFile). '"'); 
    ob_end_flush();
    readfile($zipFile);
    exit;
    die();

2 个答案:

答案 0 :(得分:4)

使用文本或十六进制编辑器打开存档,并确保您拥有' PK'文件开头的签名。 如果您在该签名之前有任何HTML,则表明您的缓冲区未被清除或者在它们不应该被刷新时被刷新,这意味着PHP ZipArchive将假定存档无效。

答案 1 :(得分:3)

导致问题的是download.php文件,这是解决方案。这是做一个OB CLEAN而不是Flush

    ///echo "<div style='padding: 50px;'>Please Wait .....</div>";
    $fileID = $_GET['id'];
    $backupLoc = "backups/";
    $sql = "SELECT * FROM backups WHERE id = '" . addslashes($fileID) .  "' LIMIT 1";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res); 
    $backupFile = $row['backupFile'];
    $zipFile = $backupLoc . "/" . $backupFile . ".zip";
    $zipSize = filesize($zipFile);

    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="' . basename($zipFile). '"'); 
    //ob_end_flush();
    ob_end_clean(); 
    readfile($zipFile);
    exit;
    die();