Home  >  Article  >  Backend Development  >  Detailed step-by-step explanation of how to decompress the contents of a zip package in php to a specified directory

Detailed step-by-step explanation of how to decompress the contents of a zip package in php to a specified directory

php中世界最好的语言
php中世界最好的语言Original
2018-04-11 13:23:421933browse

This time I will bring you a detailed explanation of the steps to decompress the contents of the zip package to the specified directory with php. What are the precautions for decompressing the contents of the zip package to the specified directory with php? The following is a practical case, let's go together take a look.

This article introduces you to the code of php decompressing the contents of the zip package to the specified directory. Friends in need can refer to it.

Directory Structure:

test

test/index.php

test/test_zip.zip
test/test_zip

<span style="font-size:14px;"><?php
	header(&#39;Content-type:text/html;charset=utf-8&#39;);
	$filename = &#39;test_zip.zip&#39;;
	$path = &#39;./test_zip.zip&#39;;
	$dir = &#39;test_zip&#39;;
	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, &#39;/&#39;) +1);
				if(!is_dir($file_name) && $file_name) {
					$save_path = $dir .&#39;/&#39;. $file_name;
					if(file_exists($save_path)) {
					echo &#39;文件夹内已存在文件 "&#39; . $file_name . &#39;" <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>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to remove null values ​​or empty elements in an array in PHP

php implements a logging function

The above is the detailed content of Detailed step-by-step explanation of how to decompress the contents of a zip package in php to a specified directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn