Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for file existence if Laravel's Blade template

I have a simple problem - how can I check if file exists in Laravel's Blade template? I tried

@if(file_exists('/covers/1.jpg')) ok @endif

But it doesn't work (covers directory is in /public). I also need to provide a variable ($game->id) to the function. Unformtunately,

@if(file_exists('/covers/'.$game->id.'.jpg')) ok @endif

doesn't work.

like image 896
Tomek Buszewski Avatar asked May 22 '13 15:05

Tomek Buszewski


2 Answers

This is working for me, favicon.ico is inside public:

@if(file_exists('favicon.ico')) 
  File exists
@else
  Could not find file
@endif

So I think you just have to use @if(file_exists('covers/1.jpg'))

like image 76
Antonio Carlos Ribeiro Avatar answered Oct 12 '22 09:10

Antonio Carlos Ribeiro


In Laravel 4 you can use:

 @if (file_exists(public_path('covers/1.jpg')))

Use public_path to get the physical path to the file.

like image 32
Ahmad Avatar answered Oct 12 '22 09:10

Ahmad