Asked  7 Months ago    Answers:  5   Viewed   43 times

Well, first of all, this is my folder structure:

images/

image1.png
image11.png
image111.png
image223.png
generate_zip.php

And this is mine generate_zip.php:

<?php

    $files = array($listfiles);

    $zipname = 'adcs.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
      $zip->addFile($file);
    }
    $zip->close();

    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename='adcs.zip'");
    header('Content-Length: ' . filesize($zipname));
    header("Location: adcs.zip");

    ?>

How to gather all the files from "images/" folder, except "generate_zip.php", and make it a downloadable .zip? In this case the "images/" folder always have a different image. Is that possible?

 Answers

41

this will ensure a file with .php extension will not be added:

   foreach ($files as $file) {
        if(!strstr($file,'.php')) $zip->addFile($file);
    }

edit: here's the full code rewritten:

<?php

    $zipname = 'adcs.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    if ($handle = opendir('.')) {
      while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
            $zip->addFile($entry);
        }
      }
      closedir($handle);
    }

    $zip->close();

    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename='adcs.zip'");
    header('Content-Length: ' . filesize($zipname));
    header("Location: adcs.zip");

    ?>
Wednesday, March 31, 2021
 
JohnnyW
answered 7 Months ago
54

The problem here is that $zip->addFile is being passed the same two parameters.

According to the documentation:

bool ZipArchive::addFile ( string $filename [, string $localname ] )

filename
The path to the file to add.

localname
local name inside ZIP archive.

This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

When you supply the second parameter, you'll want to strip the path from it when adding it to the zip archive. For example, on Unix-based systems this would look like:

$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);
Wednesday, March 31, 2021
 
zhartaunik
answered 7 Months ago
54

I think that your issue has to be something with the php version, sounds like it's this: (from php.net comments):

Some older PHP versions used to return false if zip_open failed, and newer versions return the number of error (as integer), so instead of this:

$zip = zip_open($zip_file);
if ($zip) {
  // consider zip file opened successfully
}

use this:

$zip = zip_open($zip_file);
if (is_resource($zip)) {
  // consider zip file opened successfully
}

Sounds like you are getting the first non numeric index from the array, maybe '1' as 'true' and that's why it's displaying the element '1' on your errors array

Saturday, May 29, 2021
 
BlueNile
answered 5 Months ago
29

You can add whatever you want to a zip file using ZipFile.writestr():

my_data = "<html><body><p>Hello, world!</p></body></html>"
z.writestr("hello.html", my_data)

You can also use sub-folders using / (or os.sep) as a separator:

z.writestr("site/foo/hello/index.html", my_data)
Thursday, August 12, 2021
 
EastSw
answered 3 Months ago
18

DotNetZip is much easier to use than SharpZipLib, example of zipping all files in folder :

  using (ZipFile zip = new ZipFile())
  {
    zip.AddDirectory(@"MyDocumentsProjectX", "ProjectX");
    zip.Save(zipFileToCreate);
  }

This is a an example from this page :

http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples

Sunday, September 12, 2021
 
cri_sys
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :