How to make a Zip file using PHP
Hello Everyone
In this tutorial, we are going to make a zip file using PHP. and we also unzip the zip file using PHP. we may require this when we creating applications with PHP. if you want to download a gallery as zip or you have to upload zip file to and program needs to unzip it. Both are really simple and we can do with a few lines of PHP code.
PHP has built-in extensions for dealing with compressed files. There should be no need to use system
calls for this. ZipArchive
docs is one option.
Let’s write the code.
Create a new zip file
$zip = new ZipArchive; if ($zip->open('test_new.zip', ZipArchive::CREATE) === TRUE) { // Add files to the zip file $zip->addFile('test.txt'); $zip->addFile('test.pdf'); // Add random.txt file to zip and rename it to newfile.txt $zip->addFile('random.txt', 'newfile.txt'); // All files are added, so close the zip file. $zip->close(); }
Overwrite an existing zip file
$zip = new ZipArchive; if ($zip->open('test_folder.zip', ZipArchive::CREATE) === TRUE) { // Add files to the zip file inside demo_folder $zip->addFile('text.txt', 'demo_folder/test.txt'); $zip->addFile('test.pdf', 'demo_folder/test.pdf'); // Add random.txt file to zip and rename it to newfile.txt and store in demo_folder $zip->addFile('random.txt', 'demo_folder/newfile.txt'); // Add a file demo_folder/new.txt file to zip using the text specified $zip->addFromString('demo_folder/new.txt', 'text to be added to the new.txt file'); // All files are added, so close the zip file. $zip->close(); }
Create a zip file with all files from a directory
$zip = new ZipArchive; if ($zip->open('test_dir.zip', ZipArchive::CREATE) === TRUE) { if ($handle = opendir('demo_folder')) { // Add all files inside the directory while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && !is_dir('demo_folder/' . $entry)) { $zip->addFile('demo_folder/' . $entry); } } closedir($handle); } $zip->close(); }
Okay that’s perfect. Now we have to unzip the file
Unzip a Zip File
$zip = new ZipArchive; $res = $zip->open('file.zip'); if ($res === TRUE) { $zip->extractTo('/myzips/extract_path/'); $zip->close(); echo 'woot!'; } else { echo 'doh!'; }
That’s all .. so simple right .. try this .. and hope this helps.
Download Source Code :
Post Your Questions on our forum
Post a question on Forum
Share with your friends:
How to Compress Image using PHP
In this tutorial we are going to talk about how to Compress Images using PHP, You can reduce the image […]
13 October, 2021
How to backup and download Database using PHP
Learn How to backup and download Database using PHP. Most of the time we develop some applications, the most important […]
8 October, 2021
How to make a Zip file using PHP
Hello Everyone In this tutorial, we are going to make a zip file using PHP. and we also unzip the […]
How to make pretty URL in PHP using .htaccess
Hello Everyone, Here in this tutorial, you will learn How to make a pretty URL in PHP using .htaccess. and […]
18 September, 2021
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now