2013-07-17 80 views
19

嗯,首先,这是我的文件夹结构:ZIP的所有文件目录和下载的.zip产生

images/ 

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

这是我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"); 

    ?> 

如何收集所有来自“images /”文件夹的文件,除了“generate_zip.php”,并使其可下载.zip?在这种情况下,“images /”文件夹总是有不同的图像。那可能吗?

回答

19

这将确保PHP扩展文件不会被添加:

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

编辑:这里是完整的代码重写:

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

确定,但如何列出从图像中的所有文件/目录?在数组内? –

+0

http://php.net/manual/en/function.readdir.php有这方面的需要,例#2可能是最简单的。您可以将if($ entry!=“。”&& $ entry!=“..”)扩展为if($ entry!=“。”&& entry!=“..”&&!strstr($ entry,'。 PHP')),并在该循环中添加zip,而不是我上面的示例。 – skrilled

+0

已经尝试过了,但是如何在数组里面放置while结果();? –

0

改变你的foreach循环这个以除外generate_zip.php

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

由于您只需要从某个目录创建Z的特定文件ipArchive你可以使用glob()函数来做到这一点。

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

不要使用水珠(),如果你尝试列出目录中的文件,其中很多文件存储(超过100.000)。您会收到“允许的内存大小XYZ字节耗尽...”错误。

readdir()是比较稳定的方式。 !

45

=======工作液======

包括所有子文件夹:

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

如果这是最好的解决方案,我不知道,但我+ 1'ed它导致这对我工作以及 –

+1

Yeeeeeeeeeeeeeeeeeeeeeees人你是伟大的! +1为你 – Abadis

+0

这是一个很好的答案,但它不是基于OP询问的问题,他在其中指定他只关心单个目录映像文件。只是认为应该澄清,因为任何试图完全按照OP要求做的事情会发现这些代码并不完全是这样做的。 – skrilled