2013-06-04 35 views
-2

任何機構,請告訴我如何壓縮文件夾,並在laravel4下載該壓縮文件。Laravel 4如何壓縮文件

我需要壓縮,然後自動下載zipfolder.zip

後壓縮文件夾/public/zipfolder/

我這個包安裝

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對象

+0

向我們展示你嘗試過什麼 –

+0

@Imre L重新張貼 –

回答

1

當你開始「/」的路徑變得絕對的路徑,並會導致PHP從根源看服務器的文件系統。而是使用public_path()獲得在磁盤上public路徑...

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

這是代碼爲你申請affter拉上,然後自動下載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'); 
}