Laravel 5 Multiple Download File
It is not possible to send more than one file simultaneously over the same request with the HTTP protocol. Laravel also does not support this. You have to pack the files in, for example, a zip file.
Also see
- download multiple files as zip in php
- Zipper (a handy wrapper for ZipArchive)
I want to download multiple files from my storage folder in Laravel
Looks like you didn't add any files to your Zip.
I think:
$zipper->make($filepath.'doc.zip');
Should be:
$zipper->make($filepath.'doc.zip')->add($fi)->close();
Make sure the files and zip are in the storage
folder.
Creating zip of multiple files and download in laravel
Only pure php code.
public function makeZipWithFiles(string $zipPathAndName, array $filesAndPaths): void {
$zip = new ZipArchive();
$tempFile = tmpfile();
$tempFileUri = stream_get_meta_data($tempFile)['uri'];
if ($zip->open($tempFileUri, ZipArchive::CREATE) !== TRUE) {
echo 'Could not open ZIP file.';
return;
}
// Add File in ZipArchive
foreach($filesAndPaths as $file)
{
if (! $zip->addFile($file, basename($file))) {
echo 'Could not add file to ZIP: ' . $file;
}
}
// Close ZipArchive
$zip->close();
echo 'Path:' . $zipPathAndName;
rename($tempFileUri, $zipPathAndName);
}
Laravel 5.3 multiple file uploads
It works now like this:
$files = $request->file('attachment');
if($request->hasFile('attachment'))
{
foreach ($files as $file) {
$file->store('users/' . $this->user->id . '/messages');
}
}
I had to append []
after the value of the name
attribute, so:
<input type="file" name="attachment[]" multiple>
Download button in Laravel with multiple documents ID's on button
You need to pass the $fileId
within an array for more than one id
$file = Documents::findOrFail([$fileId]);
$number_of_files = count($file);
if ($number_of_files > 1) {
// Push all the files in a zip and send the zip as download
} else {
// Send the file as a download
$file = public_path(). "/uploads/" . $file->document_path;
return Response::download($file, 'filename.pdf');
}
Related Topics
Wherejsoncontains Laravel 5.6 Not Working
Firefox Error 'No Element Found'
Get Values Stdclass Object PHP
PHP - Find If an Array Contains an Element
PHP Associative Array Key Order (Not Sort)
Class App\Http\Controllers\Usercontroller Does Not Exist
Laravel Store Array in Session
How to Resolve the Error: SQL Authentication Method Unknown in Laravel-Mysql
How to Get the Columns Names Along with Resultset in PHP/Mysql
Duplicate Data Insert in Codeigniter
Set Value of Single Object in Multidimensional Array in Twig Template
Insert a Persian Text in MySQL Table
What Does an Exclamation Mark Before a Function Really Mean in PHP
Get a Single Row from the Database Using Ajax in Codeigniter