在PHP中创建ZIP文件会出现浏览器错误,而不是脚本错误
我创建了一个代码来备份整个网站&只需单击一下就可以自动下载。下面是我的代码的样子:在PHP中创建ZIP文件会出现浏览器错误,而不是脚本错误,php,zip,Php,Zip,我创建了一个代码来备份整个网站&只需单击一下就可以自动下载。下面是我的代码的样子: if (file_exists("file.zip")) { unlink("file.zip"); } $folder_array = array(); $file_array = array(); function listFolderFiles($dir){ global $folder_array; global $file_array; $ffs = scandir($dir); f
if (file_exists("file.zip")) {
unlink("file.zip");
}
$folder_array = array();
$file_array = array();
function listFolderFiles($dir){
global $folder_array;
global $file_array;
$ffs = scandir($dir);
foreach($ffs as $ff){
if ($ff != '.' && $ff != '..') {
if (is_dir($dir.'/'.$ff)) {
$new_item = "$dir/$ff";
$new_item = str_replace('..//','',$new_item);
if ($new_item !== "stats") {
array_push($folder_array, $new_item);
listFolderFiles($dir.'/'.$ff);
}
}
else {
$new_item = "$dir/$ff";
$new_item = str_replace('..//','',$new_item);
if (($new_item !== "stats/logs") && ($new_item !== "stats/")) {
array_push($file_array, $new_item);
}
}
}
}
}
listFolderFiles('../');
$zip = new ZipArchive;
if ($zip->open('file.zip', true ? ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE) === TRUE) {
foreach($folder_array as $folder) {
$zip->addEmptyDir($folder);
}
foreach($file_array as $key => $file) {
$file_path = "../$file";
$zip->addFile($file_path, $file);
}
}
$zip->close();
$file = "file.zip";
chmod("$file", 0700);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=". $file);
readfile($file);
现在这段代码在一段时间内运行良好,但今天它似乎不想工作。问题是,这不是一个PHP脚本错误。我已经检查了错误日志,没有显示任何内容。这似乎是浏览器错误,但每个浏览器都显示不同的消息:
Chrome表示“此网页不可用”
Firefox称“连接已重置”
Internet Explorer显示“此页面无法显示”
即使出现了这些错误,ZIP文件仍在创建中,我可以从服务器下载它
以下是经过广泛研究后我尝试过的事情:
1) 我删除了代码以自动下载ZIP文件(所有代码都是在我关闭ZIP文件后编写的)。仍然会收到浏览器错误
2) 我读到有可能有太多的文件被打开,这超出了我的限制。我在代码中添加了在每200个文件之后关闭并重新打开ZIP文件。还是不行
3) 我将文件数量限制为ZIP。在500以下,一切正常。在500到1000个文件之间,代码将在部分时间内工作。有时它会通过罚款,其他它会给我的浏览器错误。大约1000年后,它根本无法正常工作
通过戈达迪主持
PHP版本是5.2.17
max_execution_time设置为240(代码从不走这么长的路,通常只需要30秒就可以运行)
内存限制设置为500米(超过所有文件大小总和的两倍)
我不知所措,我真的不知道发生了什么,因为这段代码在几周前对1500个文件运行得很好。同样,ZIP文件仍在创建中,没有PHP错误,只有浏览器出现这些错误。我不知道您的代码有什么问题,但我正在使用下面的代码,它一直在与我一起工作
function create_zip($path, $save_as)
{
if (!extension_loaded('zip'))
throw new ErrorException('Extension ZIP has not been compiled or loaded in php.');
else if(!file_exists($path))
throw new ErrorException('The file/path you want to zip doesn\'t exist!');
$zip = new ZipArchive();
if (!$zip->open($save_as, ZIPARCHIVE::CREATE))
throw new ErrorException('Could not create zip file!');
$ignore = array('.','..');
if($path == dirname($save_as))
$ignore[] = basename($save_as);
$path = str_replace('\\', '/', realpath($path));
if (is_dir($path)) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', $file);
if( in_array(substr($file, strrpos($file, '/')+1),$ignore )) continue;
$file = realpath($file);
if (is_dir($file)) {
$zip->addEmptyDir(str_replace($path . '/', '', $file . '/'));
}
else if (is_file($file)) {
$zip->addFromString(str_replace($path . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($path)) {
$zip->addFromString(basename($path), file_get_contents($path));
}
return $zip->close();
}
$zip = create_zip('/path/to/your/zip/directory', '/path/to/your/save.zip');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename('/path/to/your/save.zip').'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize('/path/to/your/save.zip'));
echo readfile('/path/to/your/save.zip');
实例化一个迭代器(在创建zip存档之前,以防在源文件夹中创建zip文件),并遍历目录以获取文件列表
您得到的HTTP响应头是什么?对我来说,这听起来像是一个超时,即使你的脚本只运行了30秒,也许有一个由GoDaddy设置的全局最大时间?你在跟他们的支持者说话吗?他们实际上没有给出任何回应。例如,这就是Chrome正在生成的内容:此网页不可用,****处的网页可能暂时关闭,或者可能已永久移动到新的网址。错误103(net::ERR_CONNECTION_ABORTED):未知错误。zip文件是否保存在正在压缩的目录中?这是正确的,它位于正在压缩的目录中。您很可能正在进入一个无休止的循环,因为您正在压缩目录,其中包含zip文件。您应该有一个特殊的忽略案例,它会将您的zip文件从添加到zip中的文件列表中排除。试试我发布的代码——它已经修复了这个问题。