Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private Temporary Storage Directory in Laravel

Tags:

laravel

With Laravel, I assume that the storage/ folder is a good place to unzip some temporary files into. So in the code, I mentioned this path: storage/tempdir. Like the following:

$zip = new ZipArchive();
$zip->open($request->excelFile->path());
$dir = "storage/tempdir";
$zip->extractTo($dir);

But the unzipped files end up in public/storage/tempdir/

This way they are accessible for public, and I don't want that.

How can I refer to storage/tempdir on both my Windows and Linux machines? tnx.

like image 834
Makan Tayebi Avatar asked May 28 '17 11:05

Makan Tayebi


2 Answers

Use storage_path() helper:

$zip->extractTo(storage_path('tempdir'));
like image 118
Alexey Mezenin Avatar answered Sep 26 '22 19:09

Alexey Mezenin


https://github.com/spatie/temporary-directory

This package offers a better solution as it allows multiple concurrent extractions without their files being mixed into the same fixed folder of storage_path('tempdir')

Using a fixed folder may work for simple cases, but once scaled up, the issues it creates are hard to troubleshoot and debug.

like image 33
bilogic Avatar answered Sep 22 '22 19:09

bilogic