2013-07-17 19 views
19

Cóż, przede wszystkim, to jest moja struktura folderów:ZIP wszystkich plików w katalogu i pobrać .zip generowane

images/ 

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

I to jest moje 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"); 

    ?> 

Jak zbierać wszystkie pliki z folderu "images /", z wyjątkiem "generate_zip.php", i sprawić, że będzie można pobrać .zip? W tym przypadku folder "images /" zawsze ma inny obraz. Czy to jest możliwe?

Odpowiedz

19

ten zapewni plik .php rozszerzenia nie zostaną dodane:

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

edit: oto pełny kod skrótu:

<?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"); 

    ?> 
+0

OK, ale w jaki sposób wymienić wszystkie pliki z katalogu images/? wewnątrz tablicy? –

+0

http://php.net/manual/en/function.readdir.php ma to, czego potrzebujesz w tym zakresie, przykład # 2 jest prawdopodobnie najłatwiejszy. możesz rozszerzyć jeśli ($ entry! = "." && $ entry! = "..") to if ($ entry! = "." && $ entry! = ".." &&! strstr ($ entry, '. php)) i zrób zip w tej pętli, zamiast w powyższym przykładzie. – skrilled

+0

Już wypróbowany, ale jak umieścić wynik while w tablicy() ;? –

0

zmiana pętla foreach do tego celu except generate_zip.php

foreach ($files as $file) { 
    if($file != "generate_zip.php"){ 
     $zip->addFile($file); 
    } 
} 
1

Ponieważ potrzebujesz tylko określonych plików z katalogu, aby utworzyć Z ipArchive możesz użyć funkcji glob(), aby to zrobić.

<?php 
    $zip = new ZipArchive; 
    $download = 'download.zip'; 
    $zip->open($download, ZipArchive::CREATE); 
    foreach (glob("images/*.png") as $file) { /* Add appropriate path to read content of zip */ 
     $zip->addFile($file); 
    } 
    $zip->close(); 
    header('Content-Type: application/zip'); 
    header("Content-Disposition: attachment; filename = $download"); 
    header('Content-Length: ' . filesize($download)); 
    header("Location: $download"); 
?> 

Nie używaj glob(), jeśli próbujesz wyświetlić listę plików w katalogu, w którym przechowywane są bardzo duże pliki (ponad 100 000). Otrzymasz błąd "Dozwolony rozmiar pamięci XYZ bajtów wyczerpany ...".

readdir() to bardziej stabilny sposób. !

45

======= Roztwór roboczy ======

obejmuje wszystkie podfoldery:

<?php 
$the_folder = 'path/foldername'; 
$zip_file_name = 'archived_name.zip'; 

class FlxZipArchive extends ZipArchive { 
     /** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;; @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private **/ 
    public function addDir($location, $name) { 
     $this->addEmptyDir($name); 
     $this->addDirDo($location, $name); 
    } // EO addDir; 

     /** Add Files & Dirs to archive;;;; @param string $location Real Location; @param string $name Name in Archive;;;;;; @author Nicolas Heimann * @access private **/ 
    private function addDirDo($location, $name) { 
     $name .= '/';   $location .= '/'; 
     // Read all Files in Dir 
     $dir = opendir ($location); 
     while ($file = readdir($dir)) { 
      if ($file == '.' || $file == '..') continue; 
      // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File(); 
      $do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile'; 
      $this->$do($location . $file, $name . $file); 
     } 
    } 
} 

$za = new FlxZipArchive; 
$res = $za->open($zip_file_name, ZipArchive::CREATE); 
if($res === TRUE) { 
    $za->addDir($the_folder, basename($the_folder)); $za->close(); 
} 
else { echo 'Could not create a zip archive';} 
?> 
+2

Jeśli jest to najlepsze rozwiązanie, nie wiem, ale dałem +1, ponieważ to zadziałało również dla mnie. –

+1

Yeeeeeeeeeeeeeeeeeeeeeee, jesteś taki wspaniały !!!! +1 dla ciebie – Abadis

+0

To jest dobra odpowiedź, ale nie opiera się na pytaniu OP, w którym określił, że dbał tylko o pliki obrazów pojedynczych katalogów.Pomyślałem, że powinno to być wyjaśnione, ponieważ każdy, kto próbuje dokładnie to, co OP zadałby sobie, znalazł ten kod, nie robi dokładnie tego. – skrilled

Powiązane problemy