简体   繁体   中英

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

Warning: scandir(WWW_ROOT/files/pdf/document,WWW_ROOT/files/pdf/document): in C:\\xampp\\htdocs\\testy\\index.php on line 9

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

Warning: scandir(): (errno 2): No such file or directory in C:\\xampp\\htdocs\\testy\\index.php on line 9

Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\testy\\index.php on line 11

solutions

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

and

Verify that the folder exists: /files/pdf/document

and I added second param - file name $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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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