【问题标题】:Php check for file_get_contents in loopphp 在循环中检查 file_get_contents
【发布时间】:2016-11-28 13:50:07
【问题描述】:

如何使用file_get_contents检查是否有内容,如果有则打印出来。

我尝试了以下方法,但没有奏效: (我使用的是 laravel 刀片语法)

    @if(file_get_contents($images[$i]->scenes) == 0) //empty
        <img class="input-preview" src="{{ asset('img/placeholder654x363.png') }}">
    @else
        <img class="input-preview" src="{{ 'data:image/jpeg;base64,'.base64_encode(file_get_contents($images[$i]->scenes)) }}">

我收到错误是因为我有两个这样的文件(字符串路径):

0 -> 没有图像 1 -> 图像

这就是为什么我试图循环并在有内容的情况下获取内容。

【问题讨论】:

  • 我猜你在找file_exisits()
  • 这种问题缺乏进一步的解释,你确定 $images 数组中还有任何东西吗?你能详细说明一下吗?
  • 数组包含2个值,第一个为空,第二个不是(保存图像路径)
  • @bub 好的,文件存在
  • @bub 你想发表你的答案吗?

标签: php arrays file loops file-get-contents


【解决方案1】:

当你使用file_get_contens返回的结果是一个字符串,那么如果这个文件为空你会得到一个空字符串

所以使用这个代码

@if(!file_exists($images[$i]->scenes) || file_get_contents($images[$i]->scenes) == false) //empty
    <img class="input-preview" src="{{ asset('img/placeholder654x363.png') }}">
@else
    <img class="input-preview" src="{{ 'data:image/jpeg;base64,'.base64_encode(file_get_contents($images[$i]->scenes)) }}">
  • !file_exists($images[$i]-&gt;scenes) 表示文件不存在
  • file_get_contents($images[$i]-&gt;scenes) == false 文件存在且内容为空

这些是不同的。

【讨论】:

  • @JohnDoe2 在这种情况下,文件已经存在并且内容为空,您将使用file_get_contents($images[$i]-&gt;scenes) == false
  • 该评论是您在编辑之前的第一个答案
  • @JohnDoe2 没问题,兄弟。希望这对你有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多