gpt4 book ai didi

PHP ZipArchive 工作正常,但现在创建无效的 zip 文件

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:56 25 4
gpt4 key购买 nike

我以前可以用这个创建动态 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 文件循环。我在其他问题上尝试了很多不同的建议(包括 addFromSting 方法)但我还没有找到解决方案。

谢谢

最佳答案

我添加了 clearstatcache(); 来帮助,以及 header('Content-Length: ' .filesize($zip_name)); 来仔细检查和生成的 zip 文件似乎总是大小合适,无论包含什么文件

我最终尝试了更多的东西,直到我偶然发现了一条线索。 foreach 中的 file_exists() 总是错误的。在启动时,它看起来是相对于域的,但在某些地方,它开始看起来是相对于脚本所在的位置。所以我为它添加了一点捕获,它开始工作了

if(file_exists($file)){
$zip->addFile($file, $name);
} else if(file_exists('../../../'.$file)){
$zip->addFile('../../../'.$file, $name);
}

仍然不知道为什么 filesize() 会起作用

关于PHP ZipArchive 工作正常,但现在创建无效的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48307292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com