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 :Â
Share with your friends:
Drag and Drop with Swapy and PHP
Hello , You might had issues with drag and drop options in your dashboard. here i found a new javascript […]
September 17, 2024
How To Generate A PDF from HTML in Laravel 11
Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]
June 22, 2024
How to get the next value of an array and loop it in the array in PHP
Do you know How to get the next value of an array and loop it in the array in PHP […]
July 28, 2023
New Open Source CRM for project Management and Invoicing
I’m excited to announce the launch of my new open source project: Gmax CRM. an invoicing and project management tool […]
December 31, 2022
Digital Marketing Toolkit
Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits
Get Free Access Now