简体   繁体   English

创建文件夹的zip文件并使用php下载

[英]create a zip file of a folder and download it using php

I have a folder named document and it contains multiple files.I want to create a zip of that folder and download it on the fly. 我有一个名为document的文件夹,其中包含多个文件。我想为该文件夹创建一个zip文件,然后即时下载。 Below is my code 下面是我的代码

$dir = WWW_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); } $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$archive); header('Content-Length: '.filesize($archive)); readfile($archive); unlink($archive);exit;

A zip file gets created but my problem is in the zip file my desired result is document folder but the zip file contains C->xampp->htdoc->app->webroot->files->pdf->document . 创建了一个zip文件,但是我的问题出在zip文件中,我想要的结果是document文件夹,但是该zip文件包含C-> xampp-> htdoc-> app-> webroot-> files-> pdf-> document Please help me 请帮我

Notice and Warning 注意和警告

Notice: Use of undefined constant WWW_ROOT - assumed 'WWW_ROOT' in C:\\xampp\\htdocs\\testy\\index.php on line 2 注意:使用未定义的常量WWW_ROOT-在第2行的C:\\ xampp \\ htdocs \\ testy \\ index.php中假定为'WWW_ROOT'

Warning: scandir(WWW_ROOT/files/pdf/document,WWW_ROOT/files/pdf/document): in C:\\xampp\\htdocs\\testy\\index.php on line 9 警告:scandir(WWW_ROOT / files / pdf / document,WWW_ROOT / files / pdf / document):位于第9行的C:\\ xampp \\ htdocs \\ testy \\ index.php中

Warning: scandir(WWW_ROOT/files/pdf/document): failed to open dir: No such file or directory in C:\\xampp\\htdocs\\testy\\index.php on line 9 警告:scandir(WWW_ROOT / files / pdf / document):无法打开dir:第9行的C:\\ xampp \\ htdocs \\ testy \\ index.php中没有此类文件或目录

Warning: scandir(): (errno 2): No such file or directory in C:\\xampp\\htdocs\\testy\\index.php on line 9 警告:scandir():(错误号2):第9行的C:\\ xampp \\ htdocs \\ testy \\ index.php中没有此类文件或目录

Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\testy\\index.php on line 11 警告:第11行的C:\\ xampp \\ htdocs \\ testy \\ index.php中为foreach()提供的参数无效

solutions 解决方案

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

and

Verify that the folder exists: /files/pdf/document 验证文件夹是否存在:/ files / pdf / document

and I added second param - file name $zip->addFile($dir.'/'.$file, $file); 然后我添加了第二个参数-文件名$ 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;

You can check this: 您可以检查以下内容:

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

if the document folder contains another sub folder with multiple files then the document folder is unable to get ziped – CHANDAN PATTNAIK 如果文档文件夹包含另一个包含多个文件的子文件夹,则该文档文件夹无法压缩– CHANDAN PATTNAIK

actually I made a mistake 其实我做错了

I now have a solution to this 我现在有一个解决方案

    /**
 * 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM