Laravel 4 如何压缩文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16924768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:53:15  来源:igfitidea点击:

Laravel 4 how to zip file

phplaravellaravel-4

提问by iDev Man

Any body please suggest me how to zip folder and download that zip file on laravel4.

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

I need to zip folder /public/zipfolder/

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

after zipped then automatic download zipfolder.zip

压缩后自动下载 zipfolder.zip

I install this package

我安装这个包

https://github.com/codeless/ziparchiveex

https://github.com/codeless/ziparchiveex

and route to

并前往

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

and i got error below

我在下面遇到错误

ZipArchive::addEmptyDir(): Invalid or unitialized Zipobject

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

回答by Phill Sparks

When you start a path with "/" the path becomes absoluteand will cause PHP to look from the root of the server's file system. Instead use public_path()to get the path on disk to public...

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

$zip->addDir(public_path().'/zipfolder/');

回答by Hi?u Tri?u Trung

This is code apply for you affter zipped then automatic download my.zip

这是压缩后为您申请的代码,然后自动下载 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');
}