配置:
1.将php_zip.dll放入\xampp\php\ext 中;
2.php.ini文件中 添加 extension=php_zip.dll,zlib.output_compression=On
//3.加入:extension=fileinfo.so
4.开启extension=php_fileinfo.dll
$zipres = $this -> zip($dstdir, $dstdir.'.zip');
if(!zipres){
$rt['status'] = 4;
$rt['info'] = "打包失败";
$this->ajaxReturn($rt,'JSON');
}
$this -> delDirAndFile($dstdir,true);
$zipname = $dstdir.'.zip';
if(!file_exists($zipname)){
$rt['status'] = 5;
$rt['info'] = "压缩包不存在";
$this->ajaxReturn($rt,'JSON');
}
$rt['status'] = 0;
$rt['info'] = "打包成功";
$rt['zipname'] = $zipname;
$this->ajaxReturn($rt,'JSON');
function zip($fromName, $toName){
if(!file_exists($fromName)){
return FALSE;
}
$zipArc = new \ZipArchive;
if(!$zipArc->open($toName, \ZipArchive::CREATE)){
return FALSE;
}
$res = is_dir($fromName) ? $zipArc->addGlob("{$fromName}/*") : $zipArc->addFile($fromName);
if(!$res){
$zipArc->close();
return FALSE;
}
return $zipArc->close();
}
function delDirAndFile($path, $delDir) {
if (is_array($path)) {
foreach ($path as $subPath)
delDirAndFile($subPath, $delDir);
}
if (is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while (false !== ( $item = readdir($handle) )) {
if ($item != "." && $item != "..")
is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
}
closedir($handle);
if ($delDir)
return rmdir($path);
}
} else {
if (file_exists($path)) {
return unlink($path);
} else {
return FALSE;
}
}
clearstatcache();
}