4

this is my first time attempting to create a ZIP File in PHP.

What I am doing is, my PHP will search for files in a certain directory, grab them all and save them into a ZIP File. The zip file will then send the file to the browser to download. I am very close, but I am stuck at a certain part.

Here is my code:

 $zip = new ZipArchive(); if ($zip->open('test.zip', ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } $myDirectory = opendir("../folder/plugins/".$id.""); while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } closedir($myDirectory); $indexCount = count($dirArray); sort($dirArray); for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ $file = "".$myDirectory."".$dirArray[$index].".zip"; $zip->addFile($file, $file) or die ("cant add file"); ; echo $dirArray[$index]; echo '</br>'; }} $zip->close()or die("cant close"); 

I am getting the 'can't close' error when attempting to close. Please help me here, I can't find what I'm doing wrong in my code. This is what it is printing:

 filename1.png filename2.png can't close 

:)

2
  • Have you looked at the error string returned by getStatusString? Commented Aug 14, 2011 at 22:38
  • In general, some code improvements: $zip->addFile($file, $file) or die ("cant add file"); ; (double semicolon), $file = "".$myDirectory."".$dirArray[$index].".zip"; can be better written as $file = "${myDirectory}${dirArray[$index]}.zip, and in general for readability it's not a good idea to combine lines of code on one line as in echo $dirArray[$index]; echo '</br>';. Commented Aug 14, 2011 at 22:43

2 Answers 2

4

Check the following line:

$zip->addFile($file, $file) 

Is this what you really want to achieve?

Sign up to request clarification or add additional context in comments.

Comments

0

Here is how I got it working:

 <?php $dirArray = array(); /* creates a compressed zip file */ $zip = new ZipArchive; if ($zip->open('dataminefiles.zip', ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } // open the current dir if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { // ignore hidden files if ($entry != "." && $entry != "..") { // only zip specific files if ( substr($entry,-3,3) == "jpg" || substr($entry,-3,3) == "pdf" || substr($entry,-3,3) == "lsx" || substr($entry,-3,3) == "xls" || substr($entry,-3,3) == "doc" || substr($entry,-3,3) == "txt" || substr($entry,-3,3) == "png" || substr($entry,-3,3) == "gif" || substr($entry,-3,3) == "peg" ) { // if allowed, add them to the array $dirArray[] = $entry; } } } closedir($handle); } $indexCount = count($dirArray); sort($dirArray); // loop through the files and add them to the zip file for($index=0; $index < $indexCount; $index++) { $file = "{$dirArray[$index]}"; $zip->addFile($file, $file); } // close the zip file $zip->close(); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.