Laravel - Image saved in storage folder not showin

2020-07-23 03:24发布

问题:

I have this code to save an image in the storage/app/uploads folder

 $image_main = Image::where('property_id', $id)->get();

$file = $request->file('file');
      $destinationPath = 'uploads';
      $i = 0;
      foreach ($file as $file1) {
          $i++;
          $extension = $file1->getClientOriginalExtension();
          $path = $file1->storeAs(
              $destinationPath, $i.time().".".$extension
          );

This is my blade file

@foreach ($image_main as $images)
        <img src="{{"/storage/app/".$images->filename}}

The file saves in the storage/app/uploads folder but it doesn't show to the user, it says file not found. am I to set anything in the filesystems.php file

回答1:

Your storage folder is not accessible from user.

You must pass by Storage::url() method:

Storage::url("/storage/app/{$images->filename}")

As:

<img src="{{ Storage::url("/storage/app/{$images->filename}") }}" alt="{{ $images->filename }}" />

https://laravel.com/docs/master/filesystem#retrieving-files



回答2:

Laravel storage filesystem is so unique. When you doing any upload it will upload in the storage/app/public directory. To make it accessible in public directory, things need to do is create symlink by run command:

php artisan storage:link

This command will symlinked storage/app/public to public/storage

Just follow the documentation about how to put and how to retrieve the file url. I am pretty sure the step you are missing is creating the symlink.

Hope it helps.



回答3:

If you are using homestead as the environment on Mac then you need to follow this:

  1. Delete your public/storage folder (if any)
  2. Follow these steps:

    1. ssh into your homestead using below command
    2. cd ~homestead
    3. vagrant up
    4. vagrant ssh
    5. cd Code/PATH_TO_YOUR_APP_ROOT_DIR
    6. php artisan storage:link
  3. Access image like this {{ asset('storage/img/myimage.jpg') }}



回答4:

You can't use the storage folder for this. You need to move the images to the public folder to access from http.

Try to create a folder call images in public folder and upload the image to that folder. Then you can access the images as follows.

<img src="{{ public_path('images/image-name.png')}}" alt="" />