How to Create Zip File using Ziparchive in PHP?

03-Apr-2023

.

Admin

How to Create Zip File using Ziparchive in PHP?

Hi Dev,

In this tutorial, I will show you how to create zip file using ziparchive in php?. let’s discuss about how to generate zip file using ziparchive in php?. you will learn php create zip file using ziparchive. it's simple example of how to save zip file using ziparchive in php?. Let's see bellow example how to create zip file using ziparchive in php.

In this example to create zip file using ZipArchive class in php. in the example, we will use open(), addFile() and close() to create a zip file.Let's see example with output.

Example :


index.php

<?php

$zip = new ZipArchive();

if ($zip->open('demo-zip.zip', ZipArchive::CREATE) == TRUE)

{

// Add files to the zip file

$zip->addFile('test.txt');

$zip->addFile('test1.txt');

//Close the zip file.

$zip->close();

}

print("demo-zip File created successfully.");

?>

Output:

After run successfully above example, you will see Zip file saved in your directory by demo-zip.zip Name.

I hope it could help you...

#PHP