Я хочу скачать файлы в формате zip.Как я могу это сделать?Я пытался, но произошла ошибка.
У меня есть имя таблицы статей:
id | volume_id | image
5 | 9 | file1.pdf
6 | 9 | file2.pdf
Я хочу скачать file1.pdf и file2.pdf в формате zip на основе тома_id
viewarticles.blade.php is:
<a href="{{ route('create-zip',['volume_id'=>$article->volume_id]) }}">Download ZIP</a>
web.php is:
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);
}
}
После нажатия на кнопку загрузки.Zip-файл обоих изображений, связанных с volume_id, должен быть загружен.
Но возникает ошибка:
ErrorException (E_WARNING)
ZipArchive::close(): Read error: No such file or directory