php 确定 Laravel 5 中是否存在文件

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

Determining If a File Exists in Laravel 5

phplaravellaravel-5laravel-5.1

提问by cyber8200

Goal: If the file exist, load the file, else load the default.png.

目标:如果文件存在,则加载文件,否则加载default.png.



I've tried

我试过了

  @if(file_exists(public_path().'/images/photos/account/{{Auth::user()->account_id}}.png'))
    <img src="/images/photos/account/{{Auth::user()->account_id}}.png" alt="">
  @else
    <img src="/images/photos/account/default.png" alt="">
  @endif


Result

结果

It kept load my defaultimage while I'm 100% sure that 1002.pngis exist.

当我 100% 确定它存在时,它一直加载我的默认图像1002.png

How do I properly check if that file is exist ?

如何正确检查该文件是否存在?

采纳答案by cyber8200

Solution

解决方案

      @if(file_exists( public_path().'/images/photos/account/'.Auth::user()->account_id.'.png' ))
        <img src="/images/photos/account/{{Auth::user()->account_id}}.png" alt="">
      @else
        <img src="/images/photos/account/default.png" alt="">
      @endif

回答by Lee

Wherever you can, try and reduce the number of ifstatements. For example, I would do the following:

只要有可能,尽量减少if语句的数量。例如,我会执行以下操作:

// User Model
public function photo()
{
    if (file_exists( public_path() . '/images/photos/account/' . $this->account_id . '.png')) {
        return '/images/photos/account/' . $this->account_id .'.png';
    } else {
        return '/images/photos/account/default.png';
    }     
}

// Blade Template
<img src="{!! Auth::user()->photo() !!}" alt="">

Makes your template cleaner and uses less code. You can also write a unit test on this method to test your statement as well :-)

使您的模板更干净并使用更少的代码。您还可以对此方法编写单元测试来测试您的语句:-)

回答by Amancho

Check if file exists on action with "File::" and pass the resut to the view

使用 "File::" 检查文件是否存在于操作中并将结果传递给视图

$result = File::exists($myfile);

回答by jOshT

In Laravel 5.5, you can use the existsmethod on the storage facade:

在 Laravel 5.5 中,你可以使用exists存储门面的方法:

https://laravel.com/docs/5.5/filesystem

https://laravel.com/docs/5.5/filesystem

$exists = Storage::disk('s3')->exists('file.jpg');

You could use a ternary expression:

您可以使用三元表达式:

$file = ($exists) ? Storage::disk('s3')->get('file.jpg') : 
         Storage::disk('s3')->get('default.jpg');

回答by ???? ????? ?????? ???? ? ?????

@if(file_exists('uploads/users-pic/'.auth()->user()->code_melli.'.jpg'))

    <img src="{{'/uploads/users-pic/'.auth()->user()->code_melli.'.jpg'}}"

class="img-circle" alt="{{auth()->user()->name}}" width="60" height="60">

@else
    <img src="/assets/images/user-4.png" width="60" height="60" class="img-circle img-corona" alt="user-pic" />
@endif

as you can see in above code when you want to check image don't use '/' at the first of you path

正如您在上面的代码中看到的那样,当您要检查图像时,请不要在第一个路径中使用“/”

 @if(file_exists('uploads/users-pic/'.auth()->user()->code_melli.'.jpg'))

回答by ???? ????? ?????? ???? ? ?????

Save the of the file in the database.If the image path exist

将文件保存在数据库中。如果图片路径存在

<?php
$path = "packages/pathoffile/img/media/" . $filename;
$media = isset($media) ? $media : ""; //If media is saved
?>
@if($media == "")
<img src='../packages/defaultimagelocation/assets/img/user.png' />
@else
<img src='../packages/originalmedialocation/img/media{{ $media }}' />
@endif