PHP解压ZIP压缩包方法、循环删除目录和文件方法!
ZIP压缩包、解压函数源码:
/**
* 解压ZIP压缩包函数(QQ:28126649)
* Email youhutong@youhutong.com
* @param $filename 源压缩包
* @param $path 解压目录
* @return array
*/
function get_zip_content($filename, $path)
{
//先判断待解压的文件是否存在
if(!file_exists($filename)){
$arr = ['code'=>false,'msg'=>"文件 $filename 不存在!"];
}else{
$str = '';
//将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到
$filename = iconv("utf-8","gb2312",$filename);
$path = iconv("utf-8","gb2312",$path);
//打开压缩包
$resource = zip_open($filename);
$i = 1;
//遍历读取压缩包里面的一个个文件
while ($dir_resource = zip_read($resource)) {
//如果能打开则继续
if (zip_entry_open($resource,$dir_resource)) {
//获取当前项目的名称,即压缩包里面当前对应的文件名
$file_name = $path.zip_entry_name($dir_resource);
//以最后一个“/”分割,再用字符串截取出路径部分
$file_path = substr($file_name,0,strrpos($file_name, "/"));
//如果路径不存在,则创建一个目录,true表示可以创建多级目录
if(!is_dir($file_path)){
mkdir($file_path,0777,true);
}
//如果不是目录,则写入文件
if(!is_dir($file_name)){
//读取这个文件
$file_size = zip_entry_filesize($dir_resource);
//最大读取12M,如果文件过大,跳过解压,继续下一个
if($file_size<(1024*1024*12)){
$file_content = zip_entry_read($dir_resource,$file_size);
file_put_contents($file_name,$file_content);
}else{
$str .= $i++." 跳过,文件过大->".iconv("gb2312","utf-8",$file_name).' ';
}
}
//关闭当前
zip_entry_close($dir_resource);
}
}
//关闭压缩包
zip_close($resource);
if(empty($data)){
$arr = ['code'=>true,'msg'=>'全部解压完成'];
}else{
$arr = ['code'=>true,'msg'=>'有部分文件超过6M已经跳过','data'=>$str];
}
}
return $arr;
}
// $zip = get_zip_content($_FILES['zip']['tmp_name'], config('path'));循环删除目录和文件函数:
/**
* 循环删除目录和文件函数(QQ:28126649)
* Email youhutong@youhutong.com
* @param $path 要删除的目录地址:最后一个要加/
* @return string
*/
function del_dir($path)
{
if(is_dir($path)){
if ($dh = @opendir($path)){
while (($obj = readdir($dh)) !== false){
if(is_dir($path.$obj)){
del_dir($path.$obj);
}else{
unlink($path.$obj);
}
}
closedir($dh);
rmdir($path);
}
}
}转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/124.html
1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意

