How to create a zip file using PHP [duplicate]

Solution 1:

$zip = new ZipArchive();

$DelFilePath="first.zip";

if(file_exists($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath)) {

        unlink ($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath); 

}
if ($zip->open($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
        die ("Could not open archive");
}
    $zip->addFile("file_path","file_name");

// close and save archive

$zip->close(); 

Here TEST is your project folder name.

You can define path as you want.

Solution 2:

Yes I have solved my problem .

Here I have just replaced the code

$zip->addFile("{$file}");

with the code

$zip->addFromString(basename($file),  file_get_contents($file));

and get my work done. :)