Pobierz wiele plików jako plik zip za pomocą php

Jak mogę pobrać wiele plików jako plik zip za pomocą php?

 93
Author: Zoe, 2009-11-18

5 answers

Możesz użyć ZipArchive klasa do tworzenia pliku ZIP i przesyłania strumieniowego do klienta. Coś w stylu:

$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

I streamować:

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

Druga linia zmusza przeglądarkę do przedstawienia Użytkownikowi pola pobierania i wyświetla monit o nazwę filename.zip. Trzecia linia jest opcjonalna, ale niektóre (głównie starsze) przeglądarki mają problemy w niektórych przypadkach bez określenia rozmiaru zawartości.

 178
Author: cletus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-31 23:26:12

To jest roboczy przykład tworzenia Zipów w PHP:

$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name,  ZipArchive::CREATE);
foreach ($files as $file) {
  echo $path = "uploadpdf/".$file;
  if(file_exists($path)){
  $zip->addFromString(basename($path),  file_get_contents($path));  
  }
  else{
   echo"file does not exist";
  }
}
$zip->close();
 26
Author: Sun Love,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-10 11:14:32

Możesz użyć xip.lib.PHP Class lib. zip.lib.php na przykład zapoznaj się z tym artykułem

 3
Author: Abdel Raoof,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-18 08:40:35

Utwórz plik zip, a następnie pobierz plik, ustawiając nagłówek, odczytaj zawartość zip i wypisz plik.

Http://www.php.net/manual/en/function.ziparchive-addfile.php

Http://php.net/manual/en/function.header.php

 1
Author: Priyank Bolia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-18 08:09:31

Jesteś gotowy do pracy z PHP zip lib, i może też używać zend zip lib,

<?PHP
// create object
$zip = new ZipArchive();   

// open archive 
if ($zip->open('app-0.09.zip') !== TRUE) {
    die ("Could not open archive");
}

// get number of files in archive
$numFiles = $zip->numFiles;

// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
    $file = $zip->statIndex($x);
    printf("%s (%d bytes)", $file['name'], $file['size']);
    print "
";    
}

// close archive
$zip->close();
?>

Http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/

I jest też php pear lib do tego http://www.php.net/manual/en/class.ziparchive.php

 1
Author: dev.meghraj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-06 13:50:51