经测试代码如下:
/**
* 解压zip
*
* @author 编程之家 jb51.cc jb51.cc
* @param type $zipfile 要解压的文件
* @param type $path 解压到该目录
* @param type $type
* @return int
*/
function emUnZip($zipfile,$path,$type = 'tpl') {
if (!class_exists('ZipArchive',FALSE)) {
return 3;//zip模块问题
}
$zip = new ZipArchive();
if (@$zip->open($zipfile) !== TRUE) {
return 2;//文件权限问题
}
$r = explode('/',$zip->getNameIndex(0),2);
$dir = isset($r[0]) ? $r[0] . '/' : '';
switch ($type) {
case 'tpl':
$re = $zip->getFromName($dir . 'header.PHP');
if (false === $re)
return -2;
break;
case 'plugin':
$plugin_name = substr($dir,-1);
$re = $zip->getFromName($dir . $plugin_name . '.PHP');
if (false === $re)
return -1;
break;
case 'backup':
$sql_name = substr($dir,-1);
if (getFileSuffix($sql_name) != 'sql')
return -3;
break;
}
if (true === @$zip->extractTo($path)) {
$zip->close();
return 0;
} else {
return 1;//文件权限问题
}
}