简体   繁体   中英

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

2 - Tried removing the include and placing the codes directly in the screenshots.php but I had no changes at all.

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

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:

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.

The thing is that all the URLs are in the database... Here is my php codes:

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

While I was searching I saw in a post a guy saying that file_exists() works for URLs in php 5+

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

I would appreciate any suggestions, thanks!

Replace following code in 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>';
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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