简体   繁体   English

如何检查图像URL是否存在于PHP中的foreach()循环中?

[英]How to check if image URL exists inside a loop of a foreach() in PHP?

EDITED: 编辑:

1 - the $search_result['image'] prints urls like: http://www.desktopwallpaperhd.net/wallpapers/21/7/wallpaper-sunset-images-back-217159.jpg 1- $search_result['image']打印如下网址: http : //www.desktopwallpaperhd.net/wallpapers/21/7/wallpaper-sunset-images-back-217159.jpg

2 - Tried removing the include and placing the codes directly in the screenshots.php but I had no changes at all. 2-尝试删除包含并将代码直接放置在screenshots.php中,但是我一点都没有改变。

3 - Figured that maybe I can't request functions because of the foreach loop, so i changed it to while($search_result, mysqli_fetch_array()) and making the sql consult as mysqli_query but the page won't load 3-考虑到可能因为foreach循环而无法请求函数,所以我将其更改为while($search_result, mysqli_fetch_array())并使sql咨询为mysqli_query但该页面无法加载

I've been searching this in google for two days and couldn't make it work and a friend of mine told me about this website, and how it works, so... To the point: 我已经在Google中搜索了两天,但无法正常运行,我的一个朋友告诉了我有关此网站及其工作方式的信息,所以……

I'm trying to check if the image URL exists and if does it returns the src of the image as the url itself, if the image URL no longer exists, or never did, the image src will change to one default. 我正在尝试检查图像URL是否存在,是否返回图像的src作为url本身,如果图像URL不再存在,或者不再存在,则图像src将更改为一个默认值。

The thing is that all the URLs are in the database... Here is my php codes: 问题是所有URL都在数据库中...这是我的php代码:

screenshots.php screenshots.php

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

sug.php 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>';
?>

While I was searching I saw in a post a guy saying that file_exists() works for URLs in php 5+ 当我搜索时,我在帖子中看到一个人说file_exists()适用于PHP 5+中的URL

Turns out that he was probably wrong because It's not working at all 原来他可能是错的,因为它根本没有用

I also tried some CURL methods but when I use CURL inside the foreach() my screenshots page doesn't loads 我也尝试了一些CURL方法,但是当我在foreach()使用CURL时,我的屏幕截图页面无法加载

I would appreciate any suggestions, thanks! 我将不胜感激任何建议,谢谢!

Replace following code in sug.php:- 在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>';
                }

with

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>';
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM