Tharindu Prabodhana
Tharindu Prabodhana

Reputation: 301

Laravel default Storage path change

I want to save image in main public folder using laravel file storeAs method. Is there anyway to change the path in using this code?

$path = $request->img->storeAs('images', 'filename.jpg');

I'm already try add new disks in filesystems.php to this

 'my' => [
            'driver' => 'local',
            'root'   => public_path('app'),
            'visibility' => 'public',
        ],

and store using this code

Storage::disk('my')->put('filename.jpg', $request->img);

but using above code it make new folder name 'filename.jpg' and save image inside that folder using random name. But I want to save that image name that 'filename.jpg'.

thank you.

Upvotes: 1

Views: 4098

Answers (2)

guizo
guizo

Reputation: 3085

It is not in the documentation but if you check storeAs method implementation (https://github.com/laravel/framework/blob/7.x/src/Illuminate/Http/UploadedFile.php#L80) you'll see that it is possible to pass an array of options as the third parameter. The disk key is used to set witch should be used. So your code would be:

$path = $request->img->storeAs('images', 'filename.jpg', ['disk' => 'my');

Bear in mind that if you are storing in a path that does not exists it is necessary to give the parent folder permission to create new directories.

Upvotes: 2

A.A Noman
A.A Noman

Reputation: 5270

You can do this very easily

$imageName = $request->file('image_name')->getClientOriginalName();
$directory = 'your/directory/path/';
$imageUrl = $directory . $imageName;
$productImage->move($directory, $imageName);

Upvotes: 3

Related Questions