eggplantSword's avatar

Unable to read file from location

I{m trying to get the file width and height but I keep getting this message

Unable to read file from location: app/public/img/reportImages/e96b4467-055d-4e5a-8c4e-bf2af6261177.png.

I know the file exists in this location

//fails
dd(Storage::get('app/public/img/reportImages/e96b4467-055d-4e5a-8c4e-bf2af6261177.png'));

//returns url
dd(Storage::url('app/public/img/reportImages/e96b4467-055d-4e5a-8c4e-bf2af6261177.png'));

The url returning means it exists but for some reason it can't read it. What am I doing wrong?

1 like
6 replies
jj15's avatar
jj15
Best Answer
Level 10

The Storage::url() method simply generates a URL, it doesn't actually check if a file exists at that path. Storage::get() attempts to find the file at the path you give relative to the base path of the disk.

By default, the "local" disk is used. It stores its files in your-project/storage/app. Using Storage::get('app/public/img/reportImages/...') would cause it to look in your-project/storage/app/app/public/img/reportImages/... which is probably not the right path.

As Snapey said, you'd need to get the full path like: Storage::disk('public')->path('img/reportImages/e96b4467-055d-4e5a-8c4e-bf2af6261177.png') (notice that the disk is being specified by first using the disk() method and that the path is relative to that of the "public" disk's base path - your-project/storage/app/public)

You may want to look at the Laravel docs on File Storage as I think there's some confusion going on here.

1 like
Snapey's avatar

@jj15 not cool to go back and expand on your earlier comment with other peoples input

1 like
Snapey's avatar

You need to learn the difference between a path and a URL. You can only play with the file by giving the full path to the file.

eg

$filepath = Storage::disk('public')->path('img/reportImages/e96b4467-055d-4e5a-8c4e-bf2af6261177.png');
1 like
eggplantSword's avatar

@snapey and @jj15

I misunderstood the method, my mistake ... but it did end up being a problem with my route. I needed img/reportImages instead of app/public/img/reportImages. Thanks

1 like

Please or to participate in this conversation.