当个人资料图片不存在时,使用file_exists函数在php中显示默认图片。

huangapple go评论64阅读模式
英文:

Displaying a default picture when the picture of the profile does not exist using file_exists php

问题

我有一个原生的 PHP 应用程序,我正在尝试升级并使用 Laravel 框架。在用户仪表板页面上,有一个根据用户ID(empid)从图片目录中获取的个人资料图片。如果用户没有个人资料图片,会显示默认图片。我以前在原生的 PHP 中使用 file_exists 方法来实现这个功能。但是我不确定为什么在 Laravel 中它现在不起作用了。

以下是我的代码:

@if (file_exists(URL::asset('assets/img/faces/'.Auth::user()->empid.'.jpeg')))
   <img alt="user-img" class="avatar avatar-xl brround" src="{{ URL::asset('assets/img/faces/'.Auth::user()->empid.'.jpeg') }}">
@else
  <img alt="user-img" class="avatar avatar-xl brround" src="{{ URL::asset('assets/img/faces/default.jpeg') }}">
@endif

这段代码没有进入 if 语句块,每次都执行 else 语句块。

英文:

I have an application in native php, and I am trying to upgrade it and use laravel framework. In the user dashboard page, there is a profile picture that is brought from the pictures directory based on the user ID (empid). If the user does not have a profile picture, there is a default picture that is displayed. I used to do it in native php using file_exists method. But I am not sure why it is not working now with laravel.
Here is my code

@if (file_exists(URL::asset(&#39;assets/img/faces/&#39;.Auth::user()-&gt;empid.&#39;.jpeg&#39;)))
   &lt;img alt=&quot;user-img&quot; class=&quot;avatar avatar-xl brround&quot; src=&quot;{{ URL::asset(&#39;assets/img/faces/&#39;.Auth::user()-&gt;empid.&#39;.jpeg&#39;) }}&quot;&gt;
@else
  &lt;img alt=&quot;user-img&quot; class=&quot;avatar avatar-xl brround&quot; src=&quot;{{ URL::asset(&#39;assets/img/faces/default.jpeg&#39;) }}&quot;&gt;
@endif

The code does not enter the if block; all the time the else block is evaluated.

答案1

得分: 3

你犯的错误是将URL传递给file_exists,因为URL::asset返回的是完整的URL而不是文件路径,所以你需要稍微调整一下,像这样:

$path = 'assets/img/faces/' . Auth::user()->empid . '.jpeg';
file_exists(public_path($path))

如果你将逻辑提取到一个函数中并返回一个头像,你的Blade代码可以简化,我会将它放在你的User模型中。

// User.php

/**
 * 如果存在,获取用户的头像,否则获取默认头像。
 * @return string
 */
public function getAvatar()
{
    $path = 'assets/img/faces/' . Auth::user()->empid . '.jpeg';

    if (file_exists(public_path($path))) {
        return asset($path);
    }

    return asset('assets/img/faces/default.jpeg');
}

然后在你的Blade模板中,移除@if @else块,并用以下简化的代码替换:

<img alt="user-img" class="avatar avatar-xl brround" src="{{ Auth::user()->getAvatar() }}">
英文:

The mistake you're doing is passing a URL to file_exists because URL::asset returns a full url not a file path so you need to tweak a little bit something like this

$path = &#39;assets/img/faces/&#39;.Auth::user()-&gt;empid.&#39;.jpeg&#39;
file_exists(public_path($path))

Your blade code can be simplified if you extracted the logic to a function that returns an avatar, I would place it in your User model.

// User.php

/**
 * Get the user&#39;s avatar if it exists otherwise gets a default avatar.
 * @return string
 */
public function getAvatar()
{
    $path = &#39;assets/img/faces/&#39; . Auth::user()-&gt;empid . &#39;.jpeg&#39;;

    if (file_exists(public_path($path))) {
        return asset($path);
    }

    return asset(&#39;assets/img/faces/default.jpeg&#39;);
}

Then in your blade remove the @if @else block and replace it with this simplified

&lt;img alt=&quot;user-img&quot; class=&quot;avatar avatar-xl brround&quot; src=&quot;{{ Auth::user()-&gt;getAvatar() }}&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月27日 15:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定