Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a zip file using PHP [duplicate]

Tags:

php

zip

I am trying to save files from one folder to another. zip folder placed in different directory. And I have written the following codes:

archive.php

<?php     $zip = new ZipArchive();     $zip->open('example.zip',  ZipArchive::CREATE);     $srcDir = "/home/sam/uploads/";     $files= scandir($srcDir);     //var_dump($files);     unset($files[0],$files[1]);     foreach ($files as $file) {         $zip->addFile("{$file}");         }     $zip->close(); ?> 

But sadly I am not able to create the .zip folder. Is there any step I missed?

like image 348
Sam Avatar asked Jul 18 '12 11:07

Sam


1 Answers

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

like image 161
Dhruvisha Avatar answered Sep 23 '22 04:09

Dhruvisha