D. Petrov
D. Petrov

Reputation: 1167

public_path() returns also the storage folder when configurating new disk in laravel 5.3

So, in my public folder there is a subfloder called images with a couple of basic images in it. What I want to do is to use laravel's Storage class to work with my future images (they're gonna be a lot and would require a lot of functionality, so I want to do it the smooth way). The problem: I'm creating a custom disk in my config/filesystems.php the following way:

'disks' => [

    //...
    'img_disk' => [
        'driver' => 'local',
        'root' => public_path(),
    ],

    //...

But then, when I try to access my picture (with the intention to put it's url as a source to my img html item), the following way:

<?php
    echo Storage::disk('img_disk')->url('window.png');
?>

It returns /storage/window.png... What I simply need is a simple manager for the sources of my images. For now I'm working locally, but it has to work as well when I update it to my actual online server. For an instance, if I simply put <img src="images/window.png">, the picture in my folder shows up with no problem. I'd like to keep it as simple as that for future and also to store my pictures in the subdirectories in the same images folder, so that I can work with them from everywhere using the Storage class. Or am I approaching completely wrong? Am I missing something or mixing something up? Any advice would be highly apreciated!

Upvotes: 0

Views: 3496

Answers (1)

hassan
hassan

Reputation: 8288

you are missing setting the image directory:

you will either set it in your file system config file , or when you are trying to call your Storage object

so , you file system file needs to be changed as follows :

'root' => public_path('images'),

or simply when you are trying to call you image :

echo Storage::disk('img_disk')->get('images/window.png');
//                              ^^^

notice that, root element is used in getting images,

to use url function, you will need to provide url element to your file system config file as follows :

'disks' => [

    //...
    'img_disk' => [
        'driver' => 'local',
        'root' => public_path('images'),
        'url' => public_path('images'), // HERE
    ],

    //...

now you can use url method easily :

echo Storage::disk('img_disk')->url('window.png');

Upvotes: 1

Related Questions