Laravel 5.6:检查文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51693314/
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 17:57:30  来源:igfitidea点击:

Laravel 5.6:Check if a file exists

phplaravelfilesystemsstorage

提问by Hammadr097

So i'm creating user profiles in laravel and want to show a generic profile image if the user hasn't uploaded his/her own profile image with the following if/else condition:

因此,我正在 laravel 中创建用户个人资料,并希望在用户未使用以下 if/else 条件上传他/她自己的个人资料图片时显示通用个人资料图片:

@if(false)
    <img src="img/uploads/avatars/{{$user->Uname}}" class="img-thumbnail" alt="{{$user->Uname}}'s Profile Pic">
@else
    <img src="img/uploads/avatars/{{$user->avatar}}" class="img-thumbnail" alt="{{$user->Uname}}'s Profile Pic">
@endif

I need to check if a file exists with the same name as the user-name of a user (uploads create a file with that name in the above specified location).The false is where the condition for file existence should be but i'm facing 2 problems that i haven't found in other solutions:

我需要检查是否存在与用户的用户名同名的文件(上传会在上面指定的位置创建一个具有该名称的文件)。错误是文件存在的条件应该是,但我是面临着我在其他解决方案中没有发现的 2 个问题:

  1. The file exists in a path outside of storage. (it exists in public folder)

  2. I want to do it without the use of storage or file facades.

  1. 该文件存在于存储外部的路径中。(它存在于公共文件夹中)

  2. 我想在不使用存储或文件外观的情况下做到这一点。

Note: Following changes have been made to filesystems.php

注意:对 filesystems.php 进行了以下更改

'local' => [
            'driver' => 'local',
            'root' => storage_path('../public/img/uploads/'),
        ]

Edit: The problem is similar to the one mentioned here but this solution didn't work for me:Determining If a File Exists in Laravel 5

编辑:问题与此处提到的问题类似,但此解决方案对我不起作用:确定 Laravel 5 中是否存在文件

Edit#2: Final Working Code:

编辑#2:最终工作代码:

@if(is_file("img/uploads/avatars/{$user->Uname}"))
<img src="img/uploads/avatars/{{$user->Uname}}" class="img-thumbnail" alt="{{$user->Uname}}'s Profile Pic">
@else
<img src="img/uploads/avatars/default.jpg" class="img-thumbnail" alt="{{$user->Uname}}'s Profile Pic">
@endif

回答by Tpojka

You should check if file exists

您应该检查文件是否存在

@if(is_file('/path/to/file.ext'))
    // code
@else
    // code
@endif

You could use laravel helpers, something like:

您可以使用 Laravel 助手,例如:

@if(is_file(public_path('img/uploads/avatars/' . $user->Uname)))
    //<img src="{{ asset('img/uploads/avatars' . $user->Uname) }}">

回答by Sabyasachi Patra

You don't have your file extension in your image src.

您的图像 src 中没有文件扩展名。

@if(file_exists('img/uploads/avatars/'.$user->Uname.'png')) 
  <img src="img/uploads/avatars/{{$user->Uname}}.png" class="img-thumbnail" alt="{{$user->Uname}}'s Profile Pic">
@else
    <img src="img/uploads/avatars/{{$user->avatar}}.png" class="img-thumbnail" alt="{{$user->Uname}}'s Profile Pic">
@endif

Use '.png' or '.jpg' as per your needs.

根据您的需要使用“.png”或“.jpg”。