ZipArchive 仅在 Windows 中无效
ZipArchive invalid in Windows only
我正在使用带有以下代码的 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 中的确切错误说:
Compressed (zipped) Folders Error
Windows cannot open the folder.
The Compressed (zipped) Folder 'C:\Users\krist\Downloads\DWV1620652983.zip' is invalid.
检查 ZIP 文件后,ZIP 内容后面有 HTML。解决方法是确保在调用 readfile
后尽快调用 exit
,这样就不会向流中写入任何其他内容。
我正在使用带有以下代码的 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 中的确切错误说:
Compressed (zipped) Folders Error
Windows cannot open the folder. The Compressed (zipped) Folder 'C:\Users\krist\Downloads\DWV1620652983.zip' is invalid.
检查 ZIP 文件后,ZIP 内容后面有 HTML。解决方法是确保在调用 readfile
后尽快调用 exit
,这样就不会向流中写入任何其他内容。