0
votes

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?

4
what is the value of $path itself? - lagbox
In the above example path returns 127.0.0.1:8000/public/… - ORStudios
no ... just $path not asset($path) - lagbox
That returns public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png - ORStudios
what are you using as the default disk? because you are telling it to save the file in a folder named public not a disk named public - lagbox

4 Answers

2
votes

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');
2
votes

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)
2
votes

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 : "";
            }


0
votes

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.