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

33

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


我已经尝试过

  @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

结果

我很确定1002.png这个文件存在,但加载页面时还是显示了默认图片。如何正确地检查这个文件是否存在?


你可以输出 public_path().'/images/photos/account/{{Auth::user()->account_id}}.png' 的结果并检查它是否是有效的路径。如果是,那么你就去查看它是否真的存在。你已经知道它不存在,因为计算机不会说谎。 - N.B.
@RyanVincent:返回 false - code-8
你确定你得到了吗?没有办法你能得到它。 - N.B.
我已经得到了字符串。你是对的。我必须修复我的连接。看看我的解决方案。感谢您的评论。 - code-8
@ihue 如果解决方案对您有用,请接受它。 - Saiyan Prince
8个回答

70

无论何时,尽量减少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="">
使您的模板更加简洁并减少代码量。您还可以编写一个单元测试来测试该方法以验证您的声明 :-)

3
public_path("/images/photos/account/{$this->account_id}.png") 是一种更短、更易读的编写条件的方式。 - totymedli

32

检查是否在 "File::" 动作中存在文件,并将结果传递给视图

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

3
File 应该从哪里导入? - Sagun Raj Lage
1
你可以直接使用\File,因为它是一个门面。 - Mihir Bhende
2
Illuminate\Support\Facades\File is a facade for Illuminate\Filesystem\Filesystem - Phillip Harrington

21

解决方案

      @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

2
@MarceloAgimóvel 这只是 Laravel 的结构。Blade 是 Laravel 提供的简单而强大的模板引擎。https://laravel.com/docs/5.6/blade - M Amir Shahzad
Blade是一种模板语言。我更喜欢将应用程序的检查和逻辑存储在类、中间件或类似可重复使用的地方,而不是视图中。 - Amancho

13

在 Laravel 5.5 中,你可以在 storage facade 上使用 exists 方法:

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

$exists = Storage::disk('s3')->exists('file.jpg');
你可以使用三目运算符:
$file = ($exists) ? Storage::disk('s3')->get('file.jpg') : 
         Storage::disk('s3')->get('default.jpg');

5
@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

正如您在上述代码中所看到的,当您想要检查图像时,请不要在路径的开头使用“/”。
 @if(file_exists('uploads/users-pic/'.auth()->user()->code_melli.'.jpg'))

1
将文件的路径保存到数据库。如果图片路径存在。
<?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

1
if (Storage::disk('<your storage disk >')->exists('<path (from disk to file existing directory name)>/<filename>') {
     Storage::disk('<your storage disk >')->delete('<path (from disk to file existing directory name)>/<filename>');
}

//Sample code
    
$post = Post::findOrFail(1);
    
if (Storage::disk('public')->exists('post/images/'.$post->image)) {
     Storage::disk('public')->delete('post/images/'.$post->image));
}

1
if( !(Storage::exists('order/'.$details['photo'])) )
{
   Storage::copy($details['photo'], 'order/'.$details['photo']);
}

use Storage;

控制器顶部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接