如果您需要您的用戶支持多文件下載的話,最好的辦法是創(chuàng)建一個(gè)壓縮包并提供下載。下面通過本文給大家看下在 Laravel 中的實(shí)現(xiàn)。
事實(shí)上,這不是關(guān)于 Laravel 的,而是和 PHP 的關(guān)聯(lián)更多,我們準(zhǔn)備使用從 PHP 5.2 以來就存在的 ZipArchive 類 ,如果要使用,需要確保php.ini 中的 ext-zip 擴(kuò)展開啟。
任務(wù) 1: 存儲(chǔ)用戶的發(fā)票文件到 storage/invoices/aaa001.pdf
下面是代碼展示:
$zip_file = 'invoices.zip'; // 要下載的壓縮包的名稱 // 初始化 PHP 類 $zip = new \ZipArchive(); $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); $invoice_file = 'invoices/aaa001.pdf'; // 添加文件:第二個(gè)參數(shù)是待壓縮文件在壓縮包中的路徑 // 所以,它將在 ZIP 中創(chuàng)建另一個(gè)名為 "storage/" 的路徑,并把文件放入目錄。 $zip->addFile(storage_path($invoice_file), $invoice_file); $zip->close(); // 我們將會(huì)在文件下載后立刻把文件返回原樣 return response()->download($zip_file);
例子很簡(jiǎn)單,對(duì)嗎?
*
任務(wù) 2: 壓縮 全部 文件到 storage/invoices 目錄中
Laravel 方面不需要有任何改變,我們只需要添加一些簡(jiǎn)單的 PHP 代碼來迭代這些文件。
$zip_file = 'invoices.zip'; $zip = new \ZipArchive(); $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); $path = storage_path('invoices'); $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); foreach ($files as $name => $file) { // 我們要跳過所有子目錄 if (!$file->isDir()) { $filePath = $file->getRealPath(); // 用 substr/strlen 獲取文件擴(kuò)展名 $relativePath = 'invoices/' . substr($filePath, strlen($path) + 1); $zip->addFile($filePath, $relativePath); } } $zip->close(); return response()->download($zip_file);
到這里基本就算完成了。你看,你不需要任何 Laravel 的擴(kuò)展包來實(shí)現(xiàn)這個(gè)壓縮方式。
PS:下面看下laravel從入門到精通之 文件處理 壓縮/解壓zip
1:將此軟件包添加到所需軟件包列表中composer.json
"chumper/zipper": "1.0.x"
2:命令行執(zhí)行
composer update
3:配置app/config/app.php
add to providers Chumper\Zipper\ZipperServiceProvider::class add to aliases 'Zipper' => Chumper\Zipper\Zipper::class
4:遍歷文件打包至壓縮包
$files = Array(); foreach ($student as $key => $data) { if ($data->photopath != null) { $check = glob(storage_path('photo/' . $data->photopath)); $files = array_merge($files, $check); } } Zipper::make(storage_path() . '/systemImg/' . $name)->add($files)->close();
5:讀取壓縮包文件
Zipper::make( storage_path() . '/photo/photos')->extractTo(storage_path('temp')); $zip = new \ZipArchive();//方法2:流處理,新建一個(gè)ZipArchive的對(duì)象 $logFiles = Zipper::make($path)->listFiles('/\.png$/i'); if ($zip->open($path) === TRUE) { foreach ($logFiles as $key) { $stream = $zip->getStream($key); $str = stream_get_contents($stream); //這里注意獲取到的文本編碼 $name = iconv("utf-8", "gb2312//IGNORE", $key); file_put_contents(storage_path() . '\temp\\' . $name, $str); } } else { return '{"statusCode":"300", "message":"上傳失敗,請(qǐng)檢查照片"}'; }
總結(jié)
以上所述是小編給大家介紹的Laravel 中創(chuàng)建 Zip 壓縮文件并提供下載的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
標(biāo)簽:遼陽 甘肅 韶關(guān) 十堰 昭通 涼山 梅河口 九江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel 中創(chuàng)建 Zip 壓縮文件并提供下載的實(shí)現(xiàn)方法》,本文關(guān)鍵詞 Laravel,中,創(chuàng)建,Zip,壓縮,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。