<html>
<head>
<title>打包操作中…</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
</head>
<body>
<?php
set_time_limit(0);
$zip = new ZipArchive();
$filename = “./”.date(“Y-m-d-H-i-s-“).md5(rand(10000,99999)).”_backup.zip”;
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
die(“当前目录不可写。创建文件失败,请检查目录权限。”);
}
$files = listdir();
foreach($files as $path){
$zip->addFile($path,str_replace(“./”,””,str_replace(“\\”,”/”,$path)));
}
echo “打包完成,共处理” . $zip->numFiles . “个文件。<br />请到当前目录下检查以日期为前缀命名的zip文件!”;
$zip->close();
function listdir($start_dir=’.’) {
$files = array();
if (is_dir($start_dir)) {
$fh = opendir($start_dir);
while (($file = readdir($fh)) !== false) {
if(strcmp($file, ‘.’)==0 || strcmp($file, ‘..’)==0){
continue;
}
$filepath = $start_dir . ‘/’ . $file;
if(is_dir($filepath)){
$files = array_merge($files, listdir($filepath));
}else{
array_push($files, $filepath);
}
}
closedir($fh);
}else{
$files = false;
}
return $files;
}
?>
</body>
</html>
转载请注明:无趣的人生也产生有意思的事件 » PHP在线文件压缩