【问题标题】:create a zip file of a folder and download it using php创建一个文件夹的 zip 文件并使用 php 下载它
【发布时间】:2015-06-16 06:59:59
【问题描述】:

我有一个名为 document 的文件夹,其中包含多个文件。我想创建该文件夹的 zip 并即时下载。下面是我的代码

 $dir = WWW_ROOT.'/files/pdf/document';
$archive = '我的文档.zip'; $zip = new ZipArchive; $zip->open($archive, ZipArchive::CREATE); $files = scandir($dir); unset($files[0], $files[1]); foreach ($files as $file) { $zip->addFile($dir.'/'.$file); } $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$archive); header('Content-Length: '.filesize($archive)); readfile($archive); unlink($archive);exit;

创建了一个 zip 文件,但我的问题在 zip 文件中,我想要的结果是 document folder 但 zip 文件包含 C->xampp->htdoc->app->webroot ->文件->pdf->文档。请帮帮我

【问题讨论】:

标签: php download compression zip


【解决方案1】:

通知和警告

注意:使用未定义的常量 WWW_ROOT - 假定为“WWW_ROOT” C:\xampp\htdocs\testy\index.php 在第 2 行

警告: 扫描目录(WWW_ROOT/files/pdf/document,WWW_ROOT/files/pdf/document):在 C:\xampp\htdocs\testy\index.php 第 9 行

警告:scandir(WWW_ROOT/files/pdf/document):无法打开目录:否 第 9 行 C:\xampp\htdocs\testy\index.php 中的此类文件或目录

警告:scandir(): (errno 2): 中没有这样的文件或目录 C:\xampp\htdocs\testy\index.php 第 9 行

警告:为 foreach() 提供的参数无效 C:\xampp\htdocs\testy\index.php 第 11 行

解决方案

$dir = $_SERVER['DOCUMENT_ROOT'].'/files/pdf/document';

验证文件夹是否存在:/files/pdf/document

我添加了第二个参数 - 文件名 $zip->addFile($dir.'/'.$file, $file);

$dir = $_SERVER['DOCUMENT_ROOT'].'/files/pdf/document';
$archive = 'MyDocument.zip';


$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);
$files = scandir($dir);
unset($files[0], $files[1]);
foreach ($files as $file) {
$zip->addFile($dir.'/'.$file, $file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);exit;

你可以检查一下:

$dir = $_SERVER['DOCUMENT_ROOT'].'/files/pdf/document';
$archive = 'MyDocument.zip';


$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);
$files = scandir($dir);
unset($files[0], $files[1]);
foreach ($files as $file) {
$zip->addFile($dir.'/'.$file, $file);
}
print_r('<pre>');
print_r($dir);//path
print_r($files);//files
print_r($zip);//object zip
die;

$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);exit;

如果文件夹包含另一个包含多个文件的子文件夹,则该文件夹无法压缩 – CHANDAN PATTNAIK

其实我弄错了

我现在有办法解决这个问题

    /**
 * Created by websky
 */
class myZipper extends ZipArchive{
    protected $dir;
    protected $archive;
    protected $pathsArray;

    /**
     * @param string $dir
     * @param string $name
     */
    public function __construct($dir,$name){
        $this->dir = $dir;
        $this->archive = $name;
        $this->open($this->archive, myZipper::CREATE);
        $this->myScanDir($this->dir);
        $this->addZip();
        $this->getZip();
    }

    /**
     * @param string $dir
     */
    protected function myScanDir($dir){
        $files = scandir($dir);
        unset($files[0], $files[1]);
        foreach ($files as $file) {
            if(is_dir($dir.'/'.$file)){
                $this->myScanDir($dir.'/'.$file);
            }
        else {
                $this->pathsArray[] = array('oldpath' => $dir.'/'.$file, 'newpath'=>  (($this->dir == $dir)? $file : str_replace($this->dir.'/', '', $dir).'/'.$file));
            }
        }
    }

    protected function addZip(){
        foreach($this->pathsArray as $path){
            $this->addFile($path['oldpath'],$path['newpath']);
        }
    }

    public function getZip(){
        $this->close();
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename='.$this->archive);
        header('Content-Length: '.filesize($this->archive));
        readfile($this->archive);
        unlink($this->archive);
    }
    }

    $test = new myZipper($_SERVER['DOCUMENT_ROOT'].'/files/pdf/document', 'MyDocument.zip');

【讨论】:

  • 感谢您的回答...但是对于我的要求,答案几乎是正确的,因为文档中存在的所有文件都已压缩,但我希望将文档文件夹与文档文件夹中的文件一起压缩
  • 如果文档文件夹包含另一个包含多个文件的子文件夹,则文档文件夹无法压缩
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2011-12-27
  • 1970-01-01
  • 2012-07-15
相关资源
最近更新 更多