The first thing you’ll need to do is make sure that the php_zip.dll is enabled in your php.ini file. Then use the following PHP code:
<?php
$zip = zip_open(“myzipfile.zip”);
if(is_resource($zip)) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen(“destination_folder/”.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);
}
?>
The code will unzip the file “myzipfile.zip” to the folder “destination_folder”. Remember that the code cannot create a folder. You have to create the folder “destination_folder” first and make sure that it has enough permission to write files, unless you will get this error “failed to open stream: No such file or directory”. And if the zip file contains folders, the code will not create those folders, you’ll need to create the folders before trying to write the file. But this should be enough to get you going on your own.
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.
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.
If you got the following error “Call to undefined function zip_open()” that means that php_zip.dll library is not enabled. Edit your php.ini file, find php_zip.dll uncomment it by removing the leading semi-colon character, and save the file. You’ll need to restart you web server.