> 백엔드 개발 > PHP 튜토리얼 > Unzip files with PHP

Unzip files with PHP

WBOY
풀어 주다: 2016-06-23 14:36:00
원래의
1196명이 탐색했습니다.

 

I've been looking everywhere on how to do this, and I've been noticing lots of searches to the site looking for this. So I finally figured out how to do it, and am puting my findings here for you.

The first thing you'll need to do is make sure you have the ZZIPlib library installed. If you have WHM, goto the Apache Build page, and just build apache with the "Zip" checkbox checked. That simple.

So what I've done is created a script that unzips the file "zip.zip" to the folder "zip". You should know that if the zip file contains folders, the code below will not create those folders, but this should be enough to get you going on your own.

Unzip File PHP Code

<?php$zip = zip_open("zip.zip");if ($zip) {  while ($zip_entry = zip_read($zip)) {    $fp = fopen("zip/".zip_entry_name($zip_entry), "w");    if (zip_entry_open($zip, $zip_entry, "r")) {      $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));      fwrite($fp,"$buf");      zip_entry_close($zip_entry);      fclose($fp);    }  }  zip_close($zip);}?>
로그인 후 복사

Zip files usually have more than one file inside them, so when you open a zip file you need to go through each file. That is what the zip_read and zip_entry_read functions are doing.

zip_read is getting all the info for each file, and zip_entry_read is getting the file contents from the info returned by zip_read.

zip_entry_name returns the path of the file within the zip. If the file is in a folder inside the zip, you'll need to create the directory before trying to write the file.

So there you have it: How to unzip a zip archive with PHP!

 

From: http://www.timlinden.com/blog/website-development/unzip-files-with-php/

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿