I changed
$file = Storage::get($name);
to
$file = Storage::url($name);
And that seemed to get the path but now I can't download it I get this error
The file "C:\xampp\htdocs\u\public\ReportesTodos.zip" does not exist
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to eventually use this code as a Job but for now I'm testing it as a regular function in a controller just to make sure it works. All the files are in the storage/app/public folder this is what I have so far. I'm following a tutorial but it doesn't seem to be the correct code.
public function getZip()
{
$zip = new ZipArchive;
$fileName = 'ReportesTodos.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {
foreach (Storage::files('public') as $name) {
$file = Storage::get($name);
if ($name != 'public/.gitignore') {
$relativeNameInZipFile = basename($file);
$zip->addFile($file, $relativeNameInZipFile);
}
}
$zip->close();
}
return response()->download(public_path($fileName));
}
When I run this function I get this error
ZipArchive::addFile() expects parameter 1 to be a valid path, string given
I think I'm not getting the files correctly but I'm not sure because if I do a dd($file); it returns the name of the file but I don't think the file itself, I'm not sure what a file is supposed to look like in a dd.
What am I doing wrong?
You can use basename to achieve this.
$zip = new ZipArchive;
if (true === ($zip->open('ReportesTodos.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE))) {
foreach (Storage::allFiles('public') as $file) {
$name = basename($file);
if ($name !== '.gitignore') {
$zip->addFile(public_path('storage\' . $name), $name);
}
}
$zip->close();
}
return response()->download(public_path('ReportesTodos.zip'), 'ReportesTodos.zip');
Hope it helps.
Please or to participate in this conversation.