我曾经能够使用以下代码创建动态zip文件:
if(isset($_POST['files'])) {
$zip = new ZipArchive();
$zip_name = $_POST['name'].".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)===TRUE) {
foreach($_POST['files'] as $file){
//here $file is like 'path/to/file.pdf' with no special characters in the name
$name = basename($file);
if(file_exists($file)){
$zip->addFile($file, $name);
}
}
$zip->close();
if(file_exists($zip_name)) {
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}
}
}
但现在它创建的zip文件无效。在Windows上,你会得到一个错误消息,在Mac上你会得到一个永无止境的zip文件循环。我已经尝试了很多其他问题的不同建议(包括
addFromString
方法),但我没有找到解决方案。谢谢。
foreach($_POST['files'] as $file)
和if(file_exists($file))
感到好奇...你能包含一下这个脚本是如何开始的吗?比如使用了哪个表单。另外,你测试过之前能用的文件,它们现在还能用吗,还是现在失败了? - IncredibleHat