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.phptest/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>
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!