我想下载zip文件。我怎样才能做到这一点?我尝试过,但是发生了错误。
我的表名称是文章:
id | volume_id | image
5 | 9 | file1.pdf
6 | 9 | file2.pdf
我要基于volume_id下载zip文件中的file1.pdf和file2.pdf
viewarticles.blade.php是:
<a href="{{ route('create-zip',['volume_id'=>$article->volume_id]) }}">Download ZIP</a>
web.php是:
Route::get('create-zip/{volume_id}', 'foruserview@createzip')->name('create-zip');
控制器为:
use App\article;
use ZipArchive;
public function createzip($volume_id)
{
$articles = article::where('volume_id',$volume_id)->get();
$file= public_path(). "/storage/upload_pic/";
$zipFileName = 'AllDocuments.zip';
$zip = new ZipArchive;
if ($zip->open($file . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
foreach($articles as $article) {
$images = $article->image;
$zip->addFile($file, $images);
}
$zip->close();
}
// Set Header
$headers = array(
'Content-Type' => 'application/octet-stream',
);
$filetopath=$file.'/'.$zipFileName;
// Create Download Response
if(file_exists($filetopath)){
return response()->download($file,$zipFileName,$headers);
}
}
点击下载按钮后。应该下载与volume_id相关的两个图像的压缩文件。
但是发生错误:
ErrorException (E_WARNING)
ZipArchive::close(): Read error: No such file or directory