2013-07-17 44 views
19

Vâng, trước hết, đây là cấu trúc thư mục của tôi:ZIP tất cả các file trong thư mục và tải về .zip tạo

images/ 

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

Và đây là generate_zip.php tôi:

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

    ?> 

Làm thế nào để thu thập tất cả các tệp từ thư mục "images /", ngoại trừ "generate_zip.php" và làm cho tệp có thể tải xuống .zip? Trong trường hợp này, thư mục "images /" luôn có hình ảnh khác. Điều đó có thể không?

Trả lời

19

này sẽ đảm bảo một tập tin với phần mở rộng .php sẽ không được thêm:

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

chỉnh sửa: đây là đoạn code đầy đủ viết lại:

<?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, nhưng làm thế nào để liệt kê tất cả các tập tin từ những hình ảnh/thư mục ? bên trong mảng? –

+0

http://php.net/manual/en/function.readdir.php có những gì bạn cần trong trường hợp đó, ví dụ # 2 có lẽ là dễ nhất. bạn có thể mở rộng nếu ($ entry! = "." && $ entry! = "..") vào if ($ entry! = "." && $ entry! = ".." &&! strstr ($ entry, '. php ')) và làm zip thêm trong vòng lặp đó quá thay vì ví dụ trên của tôi. – skrilled

+0

Đã thử, nhưng làm thế nào để đặt kết quả trong khi bên trong mảng() ;? –

0

thay đổi vòng lặp foreach của bạn để này để trừ generate_zip.php

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

Vì bạn chỉ cần tệp cụ thể từ thư mục để tạo Z ipArchive bạn có thể sử dụng chức năng glob() để làm điều này.

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

Không sử dụng glob() nếu bạn cố liệt kê các tệp trong thư mục chứa nhiều tệp được lưu trữ (hơn 100.000). Bạn nhận được một lỗi "Kích thước bộ nhớ cho phép của XYZ byte cạn kiệt ...".

readdir() là cách ổn định hơn. !

45

======= giải pháp làm việc ======

bao gồm tất cả các thư mục con:

<?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

Nếu đó là giải pháp tốt nhất, tôi không biết, nhưng tôi đã +1 nó gây ra điều này làm việc cho tôi cũng như –

+1

Yeeeeeeeeeeeeeeeeeeeeeees Người bạn tuyệt vời !!!! 1 cho bạn – Abadis

+0

Đây là một câu trả lời hay, nhưng nó không dựa trên câu hỏi mà OP đã hỏi, trong đó anh ta chỉ định rằng anh ấy chỉ quan tâm đến các tệp hình ảnh thư mục đơn lẻ.Chỉ cần nghĩ rằng nên được làm rõ như bất cứ ai đang cố gắng để làm chính xác những gì OP hỏi sẽ tìm thấy mã này không làm chính xác điều đó. – skrilled

Các vấn đề liên quan