如果 Laravel 的 Blade 模板检查文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16695300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:48:15  来源:igfitidea点击:

Check for file existence if Laravel's Blade template

laravellaravel-4

提问by Tomek Buszewski

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

我有一个简单的问题 - 如何检查 Laravel 的 Blade 模板中是否存在文件?我试过

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

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

但它不起作用(covers目录在/public)。我还需要为$game->id函数提供一个变量 ( )。不幸的是,

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

doesn't work.

不起作用。

回答by Antonio Carlos Ribeiro

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

这对我有用,favicon.ico 是公开的:

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

所以我认为你只需要使用 @if(file_exists('covers/1.jpg'))

回答by Ahmad

In Laravel 4 you can use:

在 Laravel 4 中,您可以使用:

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

Use public_pathto get the physical path to the file.

使用public_path获得该文件的物理路径。