Where to save files in Laravel 5 folder structure?

I have a Laravel project that will save files (txt or csv) after doing some calculations.

I carefully monitor how to save these files. Maybe / resources / csv / ...?

Second question: what is the best way to refer to this path from within the classes? Setting abs path in .env file? Is there a laravel method that will return the resource path?

+8
source share
3 answers

/ resources is not the best place, since this folder is used for source files and is usually stored in the source code repository (e.g. git).

Files that are generated by the application are usually located somewhere in the / storage folder - just create the / storage / csv folder there.

You should never reference these files directly from your classes. Laravel is what you need - you can read more about them here: http://laravel.com/docs/master/filesystem . They make file operations (for example, read, write, add, add, delete, move, receive all files and much more ...) much easier.

Start by defining file systems in config / filesystems.php

'disks' => [ 'csv' => [ 'driver' => 'local', 'root' => storage_path().'/csv', ], ], 

Now you can read / write your CSV files using the Repository from anywhere in your code:

 Storage::disk('csv')->put('file.csv', $content); $content = Storage::disk('csv')->get('file.csv'); 
+20
source

You can save files in the storage folder.

For instance:

You can create the csv folder in the storage folder and get the path as follows:

 storage_path().'/csv'; 

You can find the storage folder in

Laravel 4.2: application> storage
Laravel 5+: in the root directory

+4
source
 public function storePhotos($data, Request $request, $requirement) { //setlocale(LC_ALL, 'en_US.UTF-8'); //for spanish names $fileUploaded = $request->file('someVarFormName'); $folder = 'docs/'; $destinationFolder = date("Ym"); $destinationFolderInServer = $folder.$destinationFolder;//.'/'; //$rules = array('file' => 'required|mimes:png,gif,jpeg,txt,pdf,doc,docx,exel,exelx'); //'required|mimes:png,gif,jpeg,txt,pdf,doc' $validator = Validator::make(array('file' => $fileUploaded), $rules); if($validator->passes()) { $originalFileName = $fileUploaded->getClientOriginalName(); $fileName = pathinfo($originalFileName, PATHINFO_FILENAME); $fileExtension = strtolower(pathinfo($originalFileName, PATHINFO_EXTENSION)); $linkFilenameTemp = strtolower(ToolText::createLink($fileName)); $linkFilename = $linkFilenameTemp.'.'.$fileExtension; //get versioin number if filename exist $i = 1; while(file_exists($destinationFolderInServer.'/'.$linkFilename)) { $linkFilename = $linkFilenameTemp.'('.$i.').'.$fileExtension; $i++; } //* quitar / si falla $upload_success = $fileUploaded->move($destinationFolderInServer, $linkFilename); $dbRegOfFile = NULL; if($requirement->have_file) $dbRegOfFile = new mdl_TramitesRequisitosFiles(); else $dbRegOfFile = mdl_TramitesRequisitosFiles::where('requisito_id', $requirement->id) ->get(); $dbRegOfFile->requisito_id = $data['requirementId']; $dbRegOfFile->publisher = $data['publisher']; $dbRegOfFile->publisher_entity = $data['publisher_entity']; $dbRegOfFile->nice_name = $fileName; $dbRegOfFile->link_file_name = $linkFilename; $dbRegOfFile->extension = $fileName; $dbRegOfFile->size = $fileUploaded->getClientSize(); $dbRegOfFile->save(); } 
0
source

All Articles