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

milest's avatar

Check for Existence of a file

With this code, my index.blade.php will load and display images:

 @foreach( $portfolios as $portfolio)

    <img src="{{Storage::disk('images')->url( $portfolio->picture )}}" height=200 width=300 }}" >

@endforeach

If the file does not exist, a default image should display:

{{Storage::disk('images')->url( 'default.jpg' )

I have not been able to devise a test for the existence of the $portfolio->picture file.

The following always returns TRUE, even if the file does not exist:

@if( file_exists( "{{Storage::disk('images')->url( $portfolio->picture )}}"  ) )
  
  Bingo!

@endif 

I'm so close I can taste it, but it's eluding me.

Any suggestions? I would be most grateful.

0 likes
4 replies
nanpaul68's avatar

You might want to try this. Assuming you are saving the file name in your database. Though in my own case, i'm using Cartalyst Sentinel to handle authentication and access role but you can use the Laravel default auth (Auth::check()) instead of the Sentinel::getUser().

@if(!Sentinel::getUser()->profile_pic)
    <img src="/public/uploads/default.png" alt="user pic">
@else
    <img src="{{ Sentinel::getUser()->profile_pic }}" alt="user pic">
@endif 

Hope this solve your problem...

milest's avatar

@ Cronix: That always returns FALSE. even if the file exists. That's why I ended upwith URL retrieval, rather than using get.

@nanpaul68: Yes, the filename is stored in the database, but currently this has nothingto do with authentication. I'm meremly want to use the test to dispaly a "Not Found" image if the file does not exist.

milest's avatar

Why does this:

Storage::disk('images')->exists( '{{$portfolio->picture}}' )

or this

Storage::disk('images')->exists( $portfolio->picture )

always return false even when $portfolio->picture exists?

1 like

Please or to participate in this conversation.