Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

747
Visualizações
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!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

  • https://github.com/jeremykenedy/laravel-auth/blob/master/routes/web.php

// Controller Example

  • https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/ProfilesController.php

// View Examples

  • https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/profiles/show.blade.php

  • https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/profiles/edit.blade.php

  • https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/partials/nav.blade.php

over 4 years ago · Santiago Trujillo Relatório

0

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;
});
over 4 years ago · Santiago Trujillo Relatório

0

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') }}">
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda