【问题标题】:ZipArchive invalid in Windows onlyZipArchive 仅在 Windows 中无效
【发布时间】:2021-05-11 02:52:01
【问题描述】:

我正在使用 ZipArchive 和以下代码来创建 zip 文件。它适用于 Mac 上的所有浏览器。但它说该文件在 Windows 计算机上的任何浏览器上都无效。我不明白为什么。我将 Windows 计算机中所谓的损坏文件通过电子邮件发送给自己,然后在我的 Mac 计算机上打开它,它运行良好。我还阅读了this thread 上的所有建议并尝试了所有建议,但没有成功。

您发现我的代码有什么问题吗?

if(extension_loaded('zip')){     
    if(isset($post['afiles']) and count($post['afiles']) > 0){   
        $zip = new ZipArchive();    
        $url = get_stylesheet_directory();
        $filepath = $url;        
        $zip_name = "DSV".time().".zip";                 
        if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){        
            $error .=  "Error";
        }
        foreach($post['afiles'] as $file){
                    
            // get the file directory url from the file ID
            $path = get_attached_file( $file ); 
            
            // add each file to the zip file 
            $zip->addFile($path, basename($path));  
    
        }
        $zip->close();
        
        ob_clean();
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"'); 
        readfile($zip_name); 
        unlink($zip_name);
        
    }else
        $error .= "* Please select file to zip <br/><br />";
}else
    $error .= "* You do not have the ZIP extension<br/><br />";

我回复basename($path) 以确认没有斜线。它只是像“this-is-my-file.docx”这样的文件名。感谢您的任何见解!

编辑:Windows 中的确切错误说:

压缩(压缩)文件夹错误
Windows 无法打开该文件夹。 压缩(压缩)文件夹“C:\Users\krist\Downloads\DWV1620652983.zip”无效。

【问题讨论】:

    标签: php zip ziparchive


    【解决方案1】:

    检查 ZIP 文件后,在 ZIP 内容之后会出现 HTML。解决方法是确保在调用 readfile 后尽快调用 exit,以便不会向流中写入任何其他内容。

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 2016-03-08
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      相关资源
      最近更新 更多