无法使用zip存档在php中创建有效的zip文件?

问题描述 投票:1回答:1

我使用ziparchive为某些文件创建一个zip,然后需要使用以下代码下载该文件。

if(count($valid_files > 0)){
    $zip = new ZipArchive();
    $zip_name = "pixels.zip";
    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
        $error .= "* Sorry ZIP creation failed at this time";
    }

    foreach($valid_files as $file){
        $zip->addFile($file);
    }

    $zip->close();
    if(file_exists($zip_name)){
        // force to download the zip
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
        ob_clean();
        flush();
        readfile($zip_name);
     //remove zip file from temp path
        unlink($zip_name);
    }

} else {
    echo "No valid files to zip";
    exit;
}

假设$valid_files包含一个数组,每个元素都是完整的文件路径。上面的代码应该创建一个包含文件的zip文件,但相反,一旦下载启动,它将继续下载,没有任何完成大小或最大大小。在某些情况下,即使文件被下载,当我们尝试解压缩文件时,它会将错误视为无效文件。无法弄清楚任何帮助非常感谢。

php ziparchive php-zip-archive
1个回答
0
投票

主要原因是在同一个文件夹中已存在巨大的zip文件,其中我用来创建一个新zip的zip名称因此由于某种原因没有创建新的zip并且这个旧的巨大的zip开始下载并且作为Marcelo在评论中提出的大小没有被提及,因此下载不成功但是在声明编码时,事情有效,二进制提到了大小并更改了zip名称或删除了以前的zip文件。

© www.soinside.com 2019 - 2024. All rights reserved.