Check for file existence if Laravel's Blade template

孤者浪人 提交于 2020-02-03 10:26:26

问题


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.


回答1:


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'))




回答2:


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.



来源:https://stackoverflow.com/questions/16695300/check-for-file-existence-if-laravels-blade-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!