微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

本文实例讲述了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常见数据库操作技巧汇总》

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐