【问题标题】:Determining If a File Exists in Laravel 5确定文件是否存在于 Laravel 5 中
【发布时间】:2015-12-01 19:33:26
【问题描述】:

目标:如果文件存在,则加载文件,否则加载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

结果

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

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

【问题讨论】:

  • 您回显public_path().'/images/photos/account/{{Auth::user()-&gt;account_id}}.png' 的结果并检查它是否是有效路径。如果是,那么你去看看它是否真的存在。你已经知道它不会,计算机不会说谎。
  • @RyanVincent:返回false
  • 你确定你明白了吗?你没有办法得到它。
  • 我得到了字符串。你是对的。我必须修复我的串联。请参阅我的解决方案。感谢您的评论。
  • @ihue 如果对您有用,请接受该解决方案

标签: php laravel laravel-5 laravel-5.1


【解决方案1】:

尽可能减少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="">

使您的模板更整洁并使用更少的代码。你也可以在这个方法上写一个单元测试来测试你的语句:-)

【讨论】:

  • public_path("/images/photos/account/{$this-&gt;account_id}.png") 是一种更短、更易读的条件编写方式。
【解决方案2】:

使用“File::”检查文件是否存在并将结果传递给视图

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

【讨论】:

  • 应该从哪里导入File
  • 你可以做 \File 作为它的脸
  • Illuminate\Support\Facades\FileIlluminate\Filesystem\Filesystem 的外观
【解决方案3】:

解决方案

      @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

【讨论】:

  • @MarceloAgimóvel 其突出的 laravel 结构。 Blade 是 Laravel 提供的简单但功能强大的模板引擎laravel.com/docs/5.6/blade
  • Blade 它是一种模板语言。我更喜欢将我的应用程序检查和逻辑存储在类、中间件或类似且可重用的东西中;不在视图中。
【解决方案4】:

在 Laravel 5.5 中,你可以在存储门面上使用 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'))
    

    【讨论】:

      【解决方案6】:

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

      <?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
      

      【讨论】:

        【解决方案7】:
        if( !(Storage::exists('order/'.$details['photo'])) )
        {
           Storage::copy($details['photo'], 'order/'.$details['photo']);
        }
        
        use Storage;
        

        控制器顶部。

        【讨论】:

          【解决方案8】:
          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));
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-07-04
            • 2011-02-12
            • 1970-01-01
            • 2016-06-20
            • 1970-01-01
            • 2013-07-28
            • 2013-05-08
            • 2020-06-25
            相关资源
            最近更新 更多