PHP实现ZIP文件自动解压与文件遍历

      发布在:后端技术      评论:0 条评论
<p>具体代码参考如下:<br/></p><pre class="brush:php;toolbar:false">//获取当前目录 $dir&nbsp;=&nbsp;dirname(__FILE__); //&nbsp;压缩文件 $temp_file=&nbsp;&nbsp;$dir.&#39;/web.zip&#39;; //&nbsp;解压文件 $zip&nbsp;=&nbsp;new&nbsp;ZipArchive(); if&nbsp;($zip-&gt;open($temp_file)&nbsp;!==&nbsp;TRUE)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception(&#39;无法打开压缩文件&#39;); } //&nbsp;创建解压目录 $extract_path&nbsp;=&nbsp;&nbsp;$dir; //&nbsp;执行解压 if&nbsp;(!$zip-&gt;extractTo($extract_path))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception(&#39;解压失败&#39;); } $zip-&gt;close(); //&nbsp;获取解压文件列表 $files&nbsp;=&nbsp;new&nbsp;RecursiveIteratorIterator( &nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;RecursiveDirectoryIterator($extract_path), &nbsp;&nbsp;&nbsp;&nbsp;RecursiveIteratorIterator::LEAVES_ONLY ); //&nbsp;显示结果 echo&nbsp;&#39;&lt;h3&gt;解压成功!文件列表:&lt;/h3&gt;&#39;; foreach&nbsp;($files&nbsp;as&nbsp;$file)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!$file-&gt;isDir())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$file-&gt;getPathname()&nbsp;.&nbsp;&#39;&lt;br&gt;&#39;; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p><br/></p>
相关文章
热门推荐