In my Larevel app, I would like to put together some files to a zip file for the client to download. I prefer not to put the zip in the public folder. so I create a "tobedownload" folder in my "/storage" folder and plan to put the zip file there. but I got Whoops error:
"ZipArchive::close(): Failure to create temporary file: No such file or directory"
My sample code is like this: (say, the file needs to be put into zip is "whatever.PNG" in the public folder. No worries, I won't use files in this location in real implementation. the example archive file is named "AllDocuments.zip" itis under storage/tobedownload folder).
public function downloadzip(){
Storage::makeDirectory('tobedownload',$mode=0775);
$path=storage_path('tobedownload');
$public_dir=public_path();
$zipFileName = 'AllDocuments.zip';
$zip=new ZipArchive();
if ($zip->open( $path. '/' .$zipFileName, ZipArchive::CREATE))
{
$zip->addFile($public_dir.'/whatever.PNG' ); //where the file needs to be add to archive
$zip->close();
}
}
But say, if I put my new zip file in the public folder,I am able to create the zip. the following example code works.
public function downloadzip(){
//seems everthing works if I put the archive in the public folder
$public_dir=public_path();
$zipFileName = 'AllDocuments.zip';
$zip=new ZipArchive();
if ($zip->open( $public_dir. '/' .$zipFileName, ZipArchive::CREATE))
{
$zip->addFile($public_dir.'/whatever.PNG' ); //where the file needs to be add to archive
$zip->close();
}
}
I suspect it is the permission issue but I already set the storage 0755. Thanks for any helps... I am on Laravel 5.6.
OK.I think I figured it out. the folder was wrong. when you make directory on storage. Storage::disk('local')->makeDirectory('yourfolder')
the folder you create is on /storage/app/yourfolder
instead of on /storage/yourfolder
.
So in my previous code, change $path=storage_path('tobedownload');
to $path=storage_path('app/tobedownload');
makes it work.