创建另一个文件夹中的文件的zip文件夹并下载它。

时间:2014-04-15 05:44:30

标签: php

 <?php
 $zip = new ZipArchive;
 $download = 'download.zip';
 $zip->open($download, ZipArchive::CREATE);

 foreach ("d://Photo_Album/shweta/*.jpg" as $file) {
    $zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
?>

这是我的代码,但是在采用路径时它没有运行。但它不接受PHP。如果有人知道plz帮助这个

3 个答案:

答案 0 :(得分:1)

试试这个

<?php
ob_start();
ini_set('memory_limit','2048M');
set_time_limit(0);
$sourcefolder = "d:/Photo_Album/shweta/"           ; // Default: "./" 
$zipfilename  = "download.zip"; // Default: "myarchive.zip"
$timeout      = 7000           ; // Default: 5000

$the_folder = $sourcefolder;
$zip_file_name =$zipfilename;


$download_file= true;
//$delete_file_after_download= true; doesnt work!!


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);
        }
    } // EO addDirDo();
}

$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';}

if ($download_file)
{
    ob_get_clean();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($zip_file_name));
    readfile($zip_file_name);

}
?>

答案 1 :(得分:0)

您可以使用DirectoryIterator

$dirName = 'D:/Photo_Album/shweta';
$dir = new DirectoryIterator($dirName);
foreach ($dir as $fileInfo) {
    if ($fileInfo->isFile() && $fileInfo->getExtension() == 'jpg')  {
        $path = $dirName . '/' . $fileInfo->getFilename();
        $zip->addFile($path);
    }
}

答案 2 :(得分:-1)

好的,然后试试这个

<?php
$zip = new ZipArchive;
$download = 'download.zip';
$zip->open($download, ZipArchive::OVERWRITE);

$zip->addGlob('d:/Photo_Album/shweta/*.jpg', 0, array('add_path' => 'images/', 'remove_all_path' => true));
$zip->close();

header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");