How to unzip or extract zip file using PHP
This post explains how to unzip or extract a folder using PHP with the help of ZipArchive class. ZipArchive is a built in class of PHP and was introduced in PHP version 5.2 . This class will help you to unzip, zip or read zip file.
Recently we have posted a tutorial for creating a zip file using php and i think you should read about it to build more understanding on zip file creation process. In case of any questions, please do comment in comment box below.
Unzip a zip file using ZipArchive Class
Lets see the complete example to unzip a file using ZipArchive Class.
Step-1 :
creates an object of the ZipArchive class.
$destination = 'extracted/'; $zip->extractTo($destination);
Complete Source Code :
This script will help you extract files from the existing zip file. Here we are extracting the files from «skptricks-zip.zip» zip file.
php //Check whether zip extension is enabled or not if(extension_loaded('zip')) $zip = new ZipArchive(); //Path of the source zip file to be extracted $source = "skptricks-zip.zip"; if ($zip->open($source) == TRUE) //Destination of extracted files and folders to be stored $destination = 'extracted/'; $zip->extractTo($destination); $zip->close(); > else echo "Failed to open the zip file!"; > > ?>
This is all about unzip or extract zip file using PHP. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
How to Extract the Zip file with PHP
You don’t need to require any other extra plugin for working with Zip files.
PHP has a ZipArchive class that allows us to create a zip file or extract the existing file.
ZipArchive class extractTo() method is used to extract the zip file that takes the destination absolute path as an argument.
Contents
1. On the same directory
I am assuming the Zip file is stored in the project root directory.
Create an object of the ZipArchive class and open the given zip file using $zip->open() method.
If it returns TRUE then extract the file to the specified path ($path) with extractTo() method by passing path value as an argument in it.
Here, I am extracting the file in project files directory.
Completed Code
// Get Project path define(‘_PATH’, dirname(__FILE__)); // Zip file name $filename = ‘zipfile.zip’; $zip = new ZipArchive; $res = $zip->open($filename); if ($res === TRUE) < // Unzip path $path = _PATH."/files/"; // Extract file $zip->extractTo($path); $zip->close(); echo ‘Unzip!’; > else
2. Specific file
With file element, you can select the zip file that you want to extract.
If a selected file is valid then pass $_FILES[‘file’][‘tmp_name’] to open() method and extract it to specified path using extractTo() method.
Completed Code
open($tmp_name); if ($res === TRUE) < // Unzip path $path = _PATH."/files/"; // Extract file $zip->extractTo($path); $zip->close(); echo 'Unzip!'; > else < echo 'failed!'; >>else < echo 'Invalid file'; >> ?>
3. Conclusion
Using the above PHP script you can extract the existing zip files at the specified location. For this, you need to pass the absolute path in extractTo() method of ZipArchive Class.
If you found this tutorial helpful then don’t forget to share.
Create a Zip File in PHP
- Create a Zip File Using PHP
- Unzip the Zip Files Using PHP
This tutorial will demonstrate creating a zip file and unzipping that file using PHP, and adding files inside a folder in the zip file.
Create a Zip File Using PHP
The example code below will create a zip file, tutorial.zip , and add files.
In our htdocs directory, we have the Files to zip folder containing two files. We will zip the directory Files to zip .
php // Enter the name of directory $pathdir = "Files to zip/"; //Create a name for the zip folder $zipcreated = "Files to zip.zip"; // Create new zip class $zip = new ZipArchive; if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) // Store the files in our `Files to zip` zip file. $dir = opendir($pathdir); while($file = readdir($dir)) if(is_file($pathdir.$file)) $zip -> addFile($pathdir.$file, $file); > > > ?>
This code creates a new zip, Files to zip in our root directory, as shown below.
We used the directory Files to zip to create the zip folder Files to zip . Our directory had two files, insert.php and Loader.php .
We should find the above files in our zip folder. Let’s take a look.
Unzip the Zip Files Using PHP
Let’s look at how you can unzip the zip file with PHP.
php // Create new zip class $zip = new ZipArchive; // Add zip filename which needs to unzip $zip->open('Files to zip.zip'); // Extracts to current directory $zip->extractTo('./Files to zip'); $zip->close(); ?>
We use the code to unzip Files to zip.zip to the Files to zip folder.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.