【发布时间】: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