Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to save files in Laravel 5 folder structure?

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?

like image 456
koalaok Avatar asked Aug 14 '15 08:08

koalaok


People also ask

Where are Laravel save files?

Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks".

How do I change the default storage folder in Laravel?

In Laravel 5 it this works by using $app->useStoragePath('/path/') in bootstrap/app.

How do I upload files to Laravel directly into storage folder?

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.


2 Answers

/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');
like image 161
jedrzej.kurylo Avatar answered Sep 22 '22 02:09

jedrzej.kurylo


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

like image 34
Hasan Tareque Avatar answered Sep 20 '22 02:09

Hasan Tareque