Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ronnyere's avatar

How to return the image from the Storage folder?

Controller

 public function getAccount()
    {
        return view('account', ['user' => Auth::user()]);
    }

    public function postSaveAccount(Request $request)
    {
        $this->validate($request, [
           'email' => 'required|email|max:100'
        ]);

        $user           = Auth::user();
        $old_email      = $user->email;
        $user->email    = $request['email'];

        $user->update();
        $file = $request->file('image');

        $file_email     = $request['email'] . '-' . $user->id . '.jpg';
        $old_file_email = $old_email . '-' . $user->id . '.jpg';
        $update = false;

        if (Storage::disk('local')->has($old_file_email)) {
            $old_file = Storage::disk('local')->get($old_file_email);
            Storage::disk('local')->put($file_email, $old_file);
            $update = true;
        }

        if ($file) {
            Storage::disk('local')->put($file_email, File::get($file));
        }
        
        if ($update && $old_file_email !== $file_email) {
            Storage::delete($old_file_email);
        }
        return redirect()->route('account');
    }

    public function getUserImage($file_email)
    {
        $file = Storage::disk('local')->get($file_email);
        return Response::make($file,200,[ 'Content-Type' => 'image/jpeg']);
    }

Route

Route::get('/account', [
    'uses' => 'UserController@getAccount',
    'as'     => 'account'
]);

Route::post('/upateaccount', [
    'uses' => 'UserController@postSaveAccount',
    'as'     => 'account.save'
]);

Route::get('/userimage', [
    'uses' => 'UserController@getUserImage',
    'as'     => 'account.image'
]);

//Route::get('/userimage/{file_email}', [
//    'uses' => 'UserController@getUserImage',
//    'as' => 'account.image'
//]);


View current ( it's working if I pass the route {file_email})

@if (Storage::disk('local')->has($user->email . '-' . $user->id . '.jpg'))
        <section class="row new-post">
            <div class="col-md-6 col-md-offset-3">
                <img src="{{ route('account.image', ['file_email' => $user->email . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">
            </div>
        </section>
@endif

I would like to recover the image in the View by taking the user logged in by security question of the current way I can see the image of the other user by the URL.

{{ Storage::url(Auth::user()->email)}}
//
I wanted to use something like that, can anyone help?

Repository GitLab

https://gitlab.com/ronnyere/imageUploadLaravel.git

I created this rule to protect the image plus there is a problem, the comparison of the user logged in with $ id is not even passing when it is true.

 public function getUserImage($file_email)
    {
        $user = Auth::user();
        $id = $this->getIdByFileName($file_email);
        if($user->id === $id){
          $file = Storage::disk('local')->get($file_email);
          return Response::make($file,200,[ 'Content-Type' => 'image/jpeg']);
        } else {
          // devolve o response com status 401
        }
    }

// isolar esse método privado em outra classe
private function getIdByFileName($fileName){
    $fileEmailArray = explode($fileName,"-");
        $id = explode($fileEmailArray[count($fileEmailArray)-1],".")[0];
}

0 likes
2 replies
jlrdw's avatar

I don't know about all of that, I just made an asset folder and

<img src="<?php echo asset('assets/upload/imgdogs') . '/' . $row->dogpic; ?>

zero problems.

Please or to participate in this conversation.