How can i access uploaded files in lumen?

95 Views Asked by At

I tried lot of methods available on google but nothing work. I searched on google from past few days but still no success. I am trying to upload images using lumen API and it works fine. But i am not able to access those images because images are in storage folder. I also tried commands like ln -s /var/www/storage/app /var/www/public/storage but these commands run only once and i have to run those commands manually everytime that i do not want to do. Please help me to fix this.

 if ($request->hasFile('thumbnail')) {
                $file = $request->file('thumbnail');
                $allowedfileExtension = ['png', 'jpg', 'jpeg'];

                $extension = $file->getClientOriginalExtension();

                $check = in_array($extension, $allowedfileExtension);
                if ($check) {
                    $name = time() . '.' . $file->getClientOriginalExtension();
                    $file->storeAs('images', $name);
                    $thumbnailUrl = url('storage/images/' . $name);

                    $listing->thumbnail = $thumbnailUrl;
                } else {
                    return response()->json(['message' => 'Please upload correct format of image', 'status' => 400], 400);
                }
            }

Any solution appreciated!

1

There are 1 best solutions below

2
WizsTheWizard On

Use laravel's Storage facade. The default path is set into config/filesystem.php which you can change.

use Illuminate\Support\Facades\Storage;

Storage::download('storage/images' .$name);

https://laravel.com/docs/10.x/filesystem

EDIT: Lumen also supports facades, if not by default in bootstrap/app.php : $app->withFacades();