欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Where to save files in Laravel 5 folder structure? 上传文件 子目录 有大用

How to create storage folder and sub folder using input names in laravel 5



i am need advice to create folder upload in storage, i want to make folder using 4 column textbox

  • Division
  • activity
  • year
  • sponsor

how to make folder like this

storage/app/division/year/activity/sponsor/file.txt

and the code here

storage::disk('local')->put('logic');
share
 

1 Answer  正确答案

 

Once you have sanitized and stored the input strings in the relative variables, to create the folder and upload the file you can do:

Storage::disk('local')
    ->put( $division .'/'. $activity . '/' . $year . '/' . $sponsor . '/file.txt',
           File::get( <your_file> )
);
share
 

 


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?

shareimprove this question
 

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');
shareimprove this answer
 
   
My current need is to create the file then use it on the same execution. Not to have a "permanent" file reference. Anyway the solution you provide is awesome. Thanks – koalaok Aug 14 '15 at 9:00
   
If I have multiple subdirs in csv folder. What do you suggest to do to access them directly? Better defining more specific entries in config/filesystems.php ? or chaining up like Storage::disk('csv')."/mysubdir/../" (dunno yet how to write this correctly..) – koalaok Aug 14 '15 at 10:09
1 
Include the folder in the filename you pass to put/get, e.g. ->put('folder/file.csv', $content). – jedrzej.kuryloAug 14 '15 at 10:14
 

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

shareimprove this answer
 
   
Files in storage_path() are not meant to be accessed directly. Laravel's filesystems should be used for that - laravel.com/docs/master/filesystem – jedrzej.kurylo Aug 14 '15 at 8:21
来自 http://stackoverflow.com/questions/32005110/where-to-save-files-in-laravel-5-folder-structure

i am need advice to create folder upload in storage, i want to make folder using 4 column textbox

  • Division
  • activity
  • year
  • sponsor

how to make folder like this

storage/app/division/year/activity/sponsor/file.txt

and the code here

storage::disk('local')->put('logic');
share
 

Once you have sanitized and stored the input strings in the relative variables, to create the folder and upload the file you can do:

Storage::disk('local')
    ->put( $division .'/'. $activity . '/' . $year . '/' . $sponsor . '/file.txt',
           File::get( <your_file> )
);
share
 
   
great advice, let me try – kyouzano Dec 16 '15 at 9:18
   
@kyouzano : does the solution work ? – Moppo Dec 16 '15 at 18:46
   
it's work thanks – kyouzano Dec 16 '15 at 22:19

普通分类: