Upload Images to Laravel-Lumen API

14,088

Solution 1

Storage directory is not open to user's requests, public directory is, you need to create a symbolic link from storage directory to public directory:

php artisan storage:link

This command will create a symbolic link from public/storage to storage/app/public, in this case you can store your file under storage/app/public and make it accessible from the web too:

$image = $request->file('image');
$image->storeAs('public', $name); // => storage/app/public/file.img
URL::asset('storage/'.$name); // => http://example.com/stoage/file.img

Solution 2

I would advice you try to create a folder in the public folder and store your files there. You can use base_path()."/public/uploads"

Share:
14,088
Mohammed Riyadh
Author by

Mohammed Riyadh

Updated on June 06, 2022

Comments

  • Mohammed Riyadh
    Mohammed Riyadh almost 2 years

    With my API i need to upload images from mobile app to my server and save the image path to database so i have the following issues :

    • Where to save the images? I tried to save them in the storage/app under images folder (which is work fine)

         public function fileUpload(Request $request) {
      
      
         if ($request->hasFile('image')) {
             $image = $request->file('image');
             $name = time().'.'.$image->getClientOriginalExtension();
             $destinationPath = storage_path('/app/images');
             $image->move($destinationPath, $name);
      
      
             return response()->json(['data'=>"image is uploaded"]);
         }
      

      }

      and make a symbolic link to this folder in the public folder but its not working due to access permission error which will lead to the other issue.

    • What permission should i gave to the storage folder to make the whole operation works save the images and the saved link is readable (even 777 didn't work) Access forbidden!

      sudo chmod -R 777 storage

    Any ideas will be much appreciated