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. ZipArchivedocs 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

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

Export html table to excel, pdf, csv format using Datatable

Do you know how to export html table to excel, pdf, CSV, or excel format using Datatable? If you don’t […]

August 4, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Instagram Hashtag API PHP and JavaScript Integration with RapidAPI

Do you know how to integrate Instagram Hashtag API with your website? In this tutorial, I will show you how […]

August 2, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now