Asked  2 Years ago    Answers:  5   Viewed   84 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

5

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, September 14, 2022
 
5

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);
Sunday, August 21, 2022
2

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

Monday, December 26, 2022
 
quantum
 
3

Give a try for this example and tell me the results :

-R Switch : for recurring subfolders

-AF<Type> Switch : specify the archive format

-EP1 Switch : to exclude the names of the basic folder

-DH Switch: open shared files : Can process files opened by other applications. This switch is useful if an application allows access the file read, but if all access is prohibited, the file open always fail.

@echo off
Set Winrar=%ProgramFiles%WinRARWinRAR.exe
Set MyFolder=%USERPROFILE%Favorites
Set ArchiveName=apps-1.0.2.zip
"%Winrar%" a -r -afzip -ep1 -dh "%ArchiveName%" "%MyFolder%"
pause

Edit on 29/06/2016 @13:30

For others switchs and commands you can execute this batch to open the help file of Winrar :

@echo off
Set WinrarHelp=%ProgramFiles%WinRARWinRAR.chm
Start "" "%WinrarHelp%"
Tuesday, December 13, 2022
4

You need the list of files on the FTP server. Which isn't straightforward as each FTP server might return a different format of file listing...

Anyway, the ftpgetresp.c example shows a way to do it, I think. FTP Custom CUSTOMREQUEST suggests another way.

Thursday, September 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages