How to Zip/Unzip files using PHP

Compressing files while transferring from one location to other is always a good option. In case of web experience, compressing files means reducing the file size by nice margin resulting in low bandwidth consumption as well as faster download and upload speeds.

However, you might find it tedious to zip and unzip your files manually. Don’t worry, we are here with a simple PHP script that will allow you to zip and unzip files simply executing the script.

We will be using PHP ZipArchive class for this purpose that has a lot of properties and methods which can help you compress and decompress all your files.

Initial Setup :

For the demo of zip and unzip files, prepare a folder/file structure as follows with file write permission in directories “compressed” and “uncompressed”


files” is the folder which will contain files that we will be compressing to zip

compressed” is the folder which will contain the zipped output

uncompressed” is the folder which will contain the unzipped files

zip.php” and “unzip.php” are the PHP scripts for compressing and uncompressing

Source Code :



$zip = new ZipArchive();
$zip->open('compressed/', ZipArchive::CREATE);
$zip->addFile('files/1.jpg', '1.jpg');
$zip->addFile('files/2.jpg', '2.jpg');
$zip->addFile('files/3.jpg', '3.jpg');
$zip->addFile('files/4.jpg', '4.jpg');



$zip = new ZipArchive();
$zip->open('compressed/', ZipArchive::CREATE);

Follow this video for complete guidance :

Recommended For You

About the Author: Ritesh Ghimire

Ritesh is a Computer Engineer, Web Developer and an active cricket follower.