I have my Laravel project that will save files (txt or csv) after making some computations.
I'm looking after a best practice on where to save these files. Maybe /resources/csv/...?
Second question how would it be the best way to reference this path from within classes? Setting up the abs path in .env file? Is there a laravel method that will return the path to resources?
Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks".
In Laravel 5 it this works by using $app->useStoragePath('/path/') in bootstrap/app.
You can pass disk to method of \Illuminate\Http\UploadedFile class: $file = request()->file('uploadFile'); $file->store('toPath', ['disk' => 'public']); or you can create new Filesystem disk and you can save it to that disk.
/resources are not the best place, as this folder is used for source files and is usually stored in source code repository (e.g. git).
Files that application generates usually end up somewhere in /storage folder - just create a /storage/csv folder there.
You should never reference those files directly from your classes. Laravel's filesystems are what you need - you can read more about them here: http://laravel.com/docs/master/filesystem. They make operations on the files (like read, write, prepend, append, delete, move, get all files and many more...) much simpler.
Start with defining a filesystems in your config/filesystems.php
'disks' => [
'csv' => [
'driver' => 'local',
'root' => storage_path().'/csv',
],
],
Now you can read/write your csv files via Storage facade from anywhere in your code like that:
Storage::disk('csv')->put('file.csv', $content);
$content = Storage::disk('csv')->get('file.csv');
You can save files in storage folder.
For example:
You can create a folder named csv in storage folder and get the path as follows:
storage_path().'/csv';
You can find the storage folder in
Laravel 4.2 : app>storage
Laravel 5+ : in root directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With