【问题标题】:How to check if image URL exists inside a loop of a foreach() in PHP?如何检查图像 URL 是否存在于 PHP 中的 foreach() 循环中?
【发布时间】:2015-07-20 21:01:16
【问题描述】:

编辑:

1 - $search_result['image'] 打印 URL,例如:http://www.desktopwallpaperhd.net/wallpapers/21/7/wallpaper-sunset-images-back-217159.jpg

2 - 尝试删除包含并将代码直接放在 screenshots.php 中,但我根本没有任何更改。

3 - 认为可能由于 foreach 循环我无法请求函数,所以我将其更改为 while($search_result, mysqli_fetch_array()) 并将 sql 咨询为 mysqli_query 但页面不会加载

我在 google 上搜索了两天,但无法让它工作,我的一个朋友告诉我这个网站,以及它是如何工作的,所以...... 重点:

我正在尝试检查图像 URL 是否存在,以及它是否将图像的 src 作为 url 本身返回,如果图像 URL 不再存在或从未存在,则图像 src 将更改为一个默认值。

问题是所有的 URL 都在数据库中... 这是我的php代码:

screenshots.php

<?php
    echo '<div class="dv_alb_pics" id="alb_ev_pics">';
    include('scripts/sug.php');
    echo $evs_content.'</div>';
?>

sug.php

<?php
    $images_search = $SQL->query("SELECT * FROM `screenshots` WHERE `section` = '1' AND `status` = '1' ORDER BY `date` DESC LIMIT 50;")->fetchAll();
    $evs_content .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">
        <tr>
            <td>';
                foreach($images_search as $search_result) {
                    if(file_exists($search_result['image'])) {
                        $img_source = $search_result['image'];
                    } else {
                        $img_source = 'images/none.png';
                    }
                    $evs_content .= '<div class="ss_picture_rightside"><img class="ss_p_img_rightside" src="'.$img_source.'"></div>';
                }
            $evs_content .= '</td>
        </tr>
    </table>';
?>

在搜索时,我在帖子中看到一个人说file_exists() 适用于 php 5+ 中的 URL

事实证明他可能错了,因为它根本不起作用

我也尝试了一些CURL 方法,但是当我在foreach() 中使用CURL 时,我的屏幕截图页面无法加载

如果有任何建议,我将不胜感激,谢谢!

【问题讨论】:

  • 你必须使用file_exists的绝对路径。
  • 请指出 $img_source 中的内容
  • $search_result['image'] 中存储的内容是file.jpg 还是path/to/file.jpg
  • @donald123 已编辑,谢谢! $search_result['image'] 打印一个 url
  • 从技术上讲,您可以通过简单的 JS on error 类型调用来避免所有这些废话......

标签: php url


【解决方案1】:

在 sug.php 中替换以下代码:-

foreach($images_search as $search_result) {
                    if(file_exists($search_result['image'])) {
                        $img_source = $search_result['image'];
                    } else {
                        $img_source = 'images/none.png';
                    }
                    $evs_content .= '<div class="ss_picture_rightside"><img class="ss_p_img_rightside" src="'.$img_source.'"></div>';
                }

foreach($images_search as $search_result) {
                    if (getimagesize($search_result['image']) !== false) {
                        $img_source = $search_result['image'];
                    } else {
                        $img_source = 'images/none.png';
                    }
                    $evs_content .= '<div class="ss_picture_rightside"><img class="ss_p_img_rightside" src="'.$img_source.'"></div>';
                }

【讨论】:

  • 感谢您的回答,但页面没有加载 getimagesize() 函数,实际上到目前为止在 foreach() 内没有任何函数工作......有什么想法吗?
  • 你能打印_R($search_result); ?
  • yea of​​c Array ( [id] => 6 [0] => 6 [account_id] => 2 [1] => 2 [title] => 测试图片 [2] => 测试图片[位置] => 某个地方 [3] => 某个地方 [world_id] => 0 [4] => 0 [描述] => 图片描述 [5] => 图片描述 [图片] => desktopwallpaperhd.net/wallpapers/21/7/… [6 ] => desktopwallpaperhd.net/wallpapers/21/7/… [日期] => 1437117627 [7] => 1437117627 [状态] => 1 [8] => 1 [部分] => 1 [9] => 1)
  • print_R 中还有其他结果,但它们只是彼此的副本
  • 在您的 screenshots.php 文件中包含 sug.php 文件之前,只需声明 $evs_content = "";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多