加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP解压ZIP文件到指定文件夹的方法

发布时间:2020-12-12 21:52:04 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP解压ZIP文件到指定文件夹的方法。供大家参考研究具体如下: create_dirs($dest_dir); // 对每个文件进行解压 while ($zip_entry = zip_read($zip)){ // 文件不在根目录 $pos_last_slash = strrpos(zip_entry_name($zip_entry),"/")

本篇章节讲解PHP解压ZIP文件到指定文件夹的方法。分享给大家供大家参考,具体如下:

create_dirs($dest_dir); // 对每个文件进行解压 while ($zip_entry = zip_read($zip)){ // 文件不在根目录 $pos_last_slash = strrpos(zip_entry_name($zip_entry),"/"); if ($pos_last_slash !== false){ // 创建目录 在末尾带 / $this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry),$pos_last_slash+1)); } // 打开包 if (zip_entry_open($zip,$zip_entry,"r")){ // 文件名保存在磁盘上 $file_name = $dest_dir.zip_entry_name($zip_entry); // 检查文件是否需要重写 if ($overwrite === true || $overwrite === false && !is_file($file_name)){ // 读取压缩文件的内容 $fstream = zip_entry_read($zip_entry,zip_entry_filesize($zip_entry)); @file_put_contents($file_name,$fstream); // 设置权限 chmod($file_name,0777); echo "save: ".$file_name."
"; } // 关闭入口 zip_entry_close($zip_entry); } } // 关闭压缩包 zip_close($zip); } }else{ return false; } return true; } /** * 创建目录 */ public function create_dirs($path){ if (!is_dir($path)){ $directory_path = ""; $directories = explode("/",$path); array_pop($directories); foreach($directories as $directory){ $directory_path .= $directory."/"; if (!is_dir($directory_path)){ mkdir($directory_path); chmod($directory_path,0777); } } } } } /* using: $z = new Unzip(); $z->unzip("./bootstrap-3.3.4.zip",'./unzipres/',true,false); */

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家PHP程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读