我创建了一个代码来备份整个网站& &;自动下载它在一个单一的点击。下面是我的代码:
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显示"此网页不可用"火狐浏览器显示"连接已重置"ie浏览器提示"此页面无法显示"
即使出现这些错误,ZIP文件仍在创建中,我可以从服务器下载它。
以下是我经过广泛研究后尝试过的一些方法:
1)我删除了代码以使ZIP文件自动下载(所有代码都是在我关闭ZIP文件后编写的)。仍然得到浏览器错误。
我读到可能有太多的文件被打开,超过了我的限制。我在代码中添加了在每200个文件之后关闭和重新打开ZIP文件的功能。还是不行3)我将文件的数量限制为ZIP。在500以下一切都运行良好。在500到1,000个文件之间,代码将在部分时间内工作。有时它会正常运行,有时它会给我浏览器错误。在1000左右之后,它就完全不能正常工作了。
主机是通过GoDaddy。
PHP版本为5.2.17
max_execution_time设置为240(代码永远不会这么长,通常只需要30秒运行)
memory_limit设置为500M(大于所有文件大小总和的两倍)
我很困惑,我真的不知道发生了什么,因为这段代码在几个星期前还可以很好地运行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文件是在源文件夹中创建的)并遍历目录以获得文件列表。
查看更多代码在:点击我