Courtney White
Courtney White

Reputation: 614

Laravel Managing File Path

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:

  1. Create a variable

  2. Create a new file in config directory that manages the path

  3. 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

Answers (2)

Aswita Hidayat
Aswita Hidayat

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

Shizzen83
Shizzen83

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

Related Questions