-2

任何人请建议我如何压缩文件夹并在 laravel4 上下载该 zip 文件。

我需要压缩文件夹/public/zipfolder/

压缩后自动下载zipfolder.zip

我安装这个包

https://github.com/codeless/ziparchiveex

和路线

public function show($id)
{
    //echo $id;
    # ZipArchive as usual:
    $zip = new ZipArchiveEx();
    //$zip->open('my.zip', ZIPARCHIVE::OVERWRITE);

    # Add whole directory including contents:
    $zip->addDir('/public/zipfolder/');

    # Only add the contents of the directory, but
    # not the directory-entry of "mydir" itself:
    //$zip->addDirContents('mydir');

    # Close archive (as usual):
    $zip->close();
}

我在下面收到错误

ZipArchive::addEmptyDir(): 无效或未初始化的Zip对象

4

2 回答 2

1

当您以“/”开始路径时,路径将变为绝对路径,并将导致 PHP 从服务器文件系统的根目录查找。而是用于public_path()获取磁盘上的路径以public...

$zip->addDir(public_path().'/zipfolder/');
于 2013-06-05T08:05:20.903 回答
0

这是压缩后适用于您的代码,然后自动下载 my.zip

public function show($id)
{
    //echo $id;
    # ZipArchive as usual:
    $zip = new ZipArchiveEx();
    $zip->open('my.zip', ZIPARCHIVE::OVERWRITE);

    # Add whole directory including contents:
    $zip->addDir('/public/zipfolder/');

    # Only add the contents of the directory, but
    # not the directory-entry of "mydir" itself:
    //$zip->addDirContents('mydir');

    # Close archive (as usual):
    $zip->close();

    // Stream the file to the client 
    header("Content-Type: application/zip"); 
    header("Content-Length: " . filesize('my.zip')); 
    header("Content-Disposition: attachment; filename=\"my.zip\""); 
    readfile('my.zip'); 

    unlink('my.zip');
}
于 2014-11-21T16:19:42.133 回答