This article mainly shares an example of decompressing the contents of a zip package to a specified directory in PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
Directory structure:
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>
Have you learned it yet? If you find it useful, try it now.
Related recommendations:
How to use PHP to display the images in the compressed package without decompressing it
php tutorial :How to decompress the compressed package
Generate zip compressed file through php, support file and compressed package path search
The above is the detailed content of Implementation method of decompressing zip compressed package in php to specified directory. For more information, please follow other related articles on the PHP Chinese website!