【问题标题】:How to check if a file exists in Laravel 5.2如何检查 Laravel 5.2 中是否存在文件
【发布时间】:2017-01-02 19:44:07
【问题描述】:

我尝试了以下方法来检查文件是否存在:

@if(file_exists(public_path('/user_img/'.Auth::user()->id.'.jpg')))

即使文件存在,它也会返回false

这有什么问题?

【问题讨论】:

标签: php laravel-5.2 file-exists


【解决方案1】:

首先你必须在页面顶部提及使用文件

$imagepath = "1.jpg";

$directoryPath = 'user_img/'.$imagepath;

if(File::exists($directoryPath)){
    //echo "n exist"; die();
} else {  echo "not exists";   }

【讨论】:

  • 谷歌来到这里,+1 因为它是 Laravel 解决方案,而不是默认的 PHP。
【解决方案2】:

我已通过在User id 中添加花括号来进行更正,它工作正常:

@if(file_exists(public_path().'user_img/{{ Auth::User()->id }}.jpg'))

【讨论】:

  • 我是 Laravel 新手,所以我不确定这是否可行,但问题只是语法
  • 请检查是否已回答
猜你喜欢
  • 1970-01-01
  • 2016-06-26
  • 2016-09-26
  • 1970-01-01
  • 2019-01-12
  • 2020-06-25
  • 1970-01-01
  • 2019-06-08
  • 2019-05-23
相关资源
最近更新 更多