@arun07 No, your solution is what you should be doing. You can’t create it in memory; just create it locally, move it to S3, and then delete the local file. You can even create it in your server’s temporary files directory and the operating system will clean it up for you.
Laravel - Create a zip file and store it using Laravel's Storage facade
Hi
I need to create a zip file and store it using laravels Storage facade. I wish to use the Storage facade because we might change the storage to s3 or sftp later on, so I don't want to have to rewrite this again.
I am currently using php's native ZipArchive class
$filePath = 'path/to/file/file.txt';
$fileName = 'file.txt';
$zipPath = 'storage/app/public/zips/myZip.zip';
$zip = new \ZipArchive();
if (!$zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {
throw new \Exception('Failed to create zip file');
}
$zip->addFile($filePath, $fileName);
$zip->close();
This is a highly simplified version of my code.
The problem here is that this creates the file on the local filesystem, but I wish to create the zip in memory (without creating a local/temp file) and then store it by doing something like
Storage::put( 'zips/myZip.zip' , $zip);
For now, lets assume that the zip file is really small so there wont be any out of memory or timeout issues, so any idea on how I could achieve the same using the Storage class?
One solution I came up with is first create the file with ZipArchive, fetch its content and then store it using the Storage class, and then delete the local file, but that creates a lot of unecessary additional steps and includes creating a temporary file, I am hoping for a solution in which I do not need the temporary file.
Please or to participate in this conversation.