ORStudios
ORStudios

Reputation: 3233

Laravel 6 Storage results in a 404 error when trying to fetch files

I have tried to setup an upload script in Laravel and have followed the instructions in the docs. I created a Symlink using the Laravel script and it looks like the following

storage -> /Users/username/Sites/switch/storage/app/public

The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.

$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);

and this returns

http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png

the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.

http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png

Shouldn't

echo asset($path);

be returning a url containing storage instead of public?

Upvotes: 0

Views: 4091

Answers (4)

fahim152
fahim152

Reputation: 2629

Here is the simplest and exact thing for your issue


            if(!empty($request->file('manufacturer_image_name'))){
                $path = storage_path('public/image/');

                $image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));

                //Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()  
               $manufacturer->manufacturer_image_name = isset($image_path) ?  "storage/".$image_path : "";
            }


Upvotes: 2

ORStudios
ORStudios

Reputation: 3233

Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder

This was what I ended up with.

if($request->file('manufacturer_image_name')){

                $path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));

                echo $path;

                }

$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.

Upvotes: 0

Mehedi Hassan
Mehedi Hassan

Reputation: 396

Well, there are a lot of ways to do that, pick anyone which fits you best.

// using storage_path helper
storage_path('public/' . $filename);

// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);

if (!File::exists($path)) {
   abort(404);
}

// using asset helper
asset('storage/your_folder/image.png');

// using url helper
url('storage/your_folder/image.png');

// using Storage facade
Storage::url($photoLink)

Upvotes: 2

Alec Joy
Alec Joy

Reputation: 1979

assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.

Storage::url('file.jpg');

Upvotes: 3

Related Questions