I need to create, zip, download and delete a file in one request in Laravel. I use the following code in my controller:
$pathToFile = "myFile.txt";
$content = "content";
Storage::put($pathToFile,$content);
$file = storage_path().'/app/'.$pathToFile;
$zipFile = storage_path().'/app/'.$this->name.'.zip';
Zipper::make($zipFile)->add($file);
return response()->download($zipFile);
Unfortunately the zip file is not created at the time I try to download it. Therefore I got an error that the file does not exists. However after the error response the file is created and it is available. So if I run the same controller method again, the file is downloaded.
Could you please help me with this issue. I would like to be able to create the file, to download it and delete it.
I finally fixed the issue myself. It is needed to call the close method of the Zipper, in order to create the file:
$pathToFile = "myFile.txt";
$content = "content";
Storage::put($pathToFile,$content);
$file = storage_path().'/app/'.$pathToFile;
$zipFile = storage_path().'/app/'.$this->name.'.zip';
Zipper::make($zipFile)->add($file);
Zipper::close();
return response()->download($zipFile);