-2

I found this awesome code to create a ZIP file using PHP via this link compress/archive folder using php script

<?php
// Config Vars 

$sourcefolder = "./"           ; // Default: "./" 
$zipfilename  = "myarchive.zip"; // Default: "myarchive.zip"
$timeout      = 5000           ; // Default: 5000

// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);

// set script timeout value 
ini_set('max_execution_time', $timeout);

// instantate object
$zip = new ZipArchive();

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

// close the archive
$zip->close();
echo "Archive ". $zipfilename . " created successfully.";

// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">
Download <?php echo $zipfilename?></a> 

I would like to create the zip in a particular directory.

Community
  • 1
  • 1
  • Have you tried using a complete path for the zip file name, instead of just a file name? – Robert Harvey Mar 11 '13 at 21:44
  • 1
    "I found this awesome code" - have you actually tried to understand how it works or do just want us to adjust it for you before you paste it into your app? – Gordon Mar 11 '13 at 21:45
  • But dude it's awesome – AlienWebguy Mar 11 '13 at 21:46
  • Please take the reference of this code. It will resolve your issue. $zip = new ZipArchive; $zip->open('testPDFZip.zip', ZipArchive::CREATE); foreach (glob(APPLICATION_PATH."pages/recruitment/uploads/test_pdf_folder/*") as $file) { echo "
    iteration"; $new_filename = end(explode("/",$file)); $zip->addFile($file,"emp/".$new_filename); } $zip->close();
    – Lakhan Nov 25 '14 at 07:01

1 Answers1

3
$zipfilename  = "/path/to/my/zip/folder/myarchive.zip";

Simply make sure the folder is writable.

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145