当前位置:Gxlcms > PHP教程 > php解压zip压缩包内容到指定目录步奏详解

php解压zip压缩包内容到指定目录步奏详解

时间:2021-07-01 10:21:17 帮助过:6人阅读

这次给大家带来php解压zip压缩包内容到指定目录步奏详解,php解压zip压缩包内容到指定目录的注意事项有哪些,下面就是实战案例,一起来看一下。

本文给大家介绍了php解压zip压缩包内容到指定目录的代码,有需要的朋友可以参考一下。

目录结构:

test

test/index.php
test/test_zip.zip
test/test_zip

<span style="font-size:14px;"><?php
	header('Content-type:text/html;charset=utf-8');
	$filename = 'test_zip.zip';
	$path = './test_zip.zip';
	$dir = 'test_zip';
	if(!is_dir($dir)) {
		mkdir($dir, 0777, true);//创建目录保存解压内容
	}
	if(file_exists($filename)) {
		$resource = zip_open($filename);
		while($zip = zip_read($resource)) {
			if(zip_entry_open($resource, $zip)) {
		$file_content = zip_entry_name($zip);//获得文件名,mac压缩成zip,解压需要过滤资源库隐藏文件
				$file_name = substr($file_content, strrpos($file_content, '/') +1);
				if(!is_dir($file_name) && $file_name) {
					$save_path = $dir .'/'. $file_name;
					if(file_exists($save_path)) {
					echo '文件夹内已存在文件 "' . $file_name . '" <pre />';
					}else {
						echo $file_name . '<pre />';	
						$file_size = zip_entry_filesize($zip);
						$file = zip_entry_read($zip, $file_size);
						file_put_contents($save_path, $file);
						zip_entry_close($zip);
					}
					
				}
			}
		}
		zip_close($resource);
	}</span>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

PHP怎么移除数组中的空值或者空元素

php实现一个日志功能

以上就是php解压zip压缩包内容到指定目录步奏详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行