Reputation: 614
In my application users can upload files. In my controller I have the following:
//...
$request->file->storeAs('/public/user_files',$fileName);
I don't like how I have to hard code the path /public/user_files
, what is the proper way to manage file paths? I could simply create a variable but is there any better ways for maximum maintainability?
From my understanding I have a few options:
Create a variable
Create a new file in config
directory that manages the path
Create a disk in filesystem.php
and use storeAs
with the custom disk
What is the best way to handle paths?
Upvotes: 0
Views: 776
Reputation: 159
you can use helpper public_path()
$request->file->storeAs( public_path('/user_files') ,$fileName);
doc: https://laravel.com/docs/7.x/helpers#method-public-path
Upvotes: 0
Reputation: 3529
In most cases, you should use filesystem disks as you specified in your last option, it allows a centralized flexibility.
In this case, you may use Laravel helpers such as public_path
or storage_path
, for example:
$request->file->storeAs(
storage_path('app/public'), $fileName
);
Combined to a symbolic link, it allows flexibility about public assets.
https://laravel.com/docs/7.x/filesystem#the-public-disk
Upvotes: 2