简体   繁体   中英

LARAVEL - Displaying images from the storage folder

I'm new to Laravel and was wondering if someone can help me out with a simple image upload.

I have a form that allows a user to create a profile and upload and avatar for their profile during the process. This all works perfectly. Here is the code from my controller:

if (request()->hasFile('avatar')) {

        $file = request()->file('avatar');

        $file->storeAs('avatars/' . auth()->id(), 'avatar.jpg');
    }

The image is saved within storage/app/avatars/USER-ID/avatar.jpg

I'm not sure how to display the image from this folder. I've looked for solutions but I am unable to use php artisan storage:link as it is not my server. How do I go about this without using the storage link? Can I create a link manually?

If someone could explain a solution to me that would be perfect!

Just ask if you need any code snippets.

Thanks!

You will need to access the protected files using a route the plugs into a controller that can access the files and pass it to the view.

I would also recommend using the package intervention/image it can be installed by

composer require intervention/image

See below to on accessing the image:

// Your route in web.php

// Public route to show user avatar
Route::get('avatars/{id}/{image}', [
    'uses'      => 'TheNameOfYourController@userProfileAvatar'
]);

// In your controller file. In this example according the name of what we have above it would be userProfileAvatar()

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class TheNameOfYourController extends Controller
{

    /**
     * Show user avatar
     *
     * @param $id
     * @param $image
     * @return string
     */
    public function userProfileAvatar($id, $image)
    {
        return Image::make(storage_path() . '/avatars/' . $id . '/' . $image)->response();
    }

}

// Your blade view to display the image

<img src="images/profile/{{ \Auth::user()->id }}/avatar.jpg" alt="{{ \Auth::user()->name }}">

Here are some references but this intances that go one step further and save the file path the database upon upload:

// Route Example

// Controller Example

// View Examples

Well if you don't have access to the server to create a symlink, you need to create a route that returns the image as response.

Something like that:

Route::get('storage/{filename}', function ($filename) 
{
   $path = storage_path('avatars/' . $filename);

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

   $file = File::get($path);
   $type = File::mimeType($path);

   $response = Response::make($file, 200);
   $response->header("Content-Type", $type);

   return $response;
});

Create a symbolic link which will connect storage/app and public/img directories, for example:

ln -s /home/laravel/public/img /home/laravel/storage/app

Then just use asset() helper to generate a link:

<img src="{{ asset('img/avatars/'.auth()->id().'/avatar.jpg') }}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM