0

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!

  • 2
    You have to use the absolute path with `file_exists`. – Sougata Bose Jul 20 '15 at 12:03
  • pls point out what in *$img_source* – donald123 Jul 20 '15 at 12:04
  • What do you store in `$search_result['image']` is it just `file.jpg` or `path/to/file.jpg` – RiggsFolly Jul 20 '15 at 12:06
  • @donald123 Edited, thanks! the `$search_result['image']` prints an url –  Jul 20 '15 at 12:07
  • Technically you could avoid all this palaver with a simple JS `on error` type call... – CD001 Jul 20 '15 at 12:07
  • take a look here http://stackoverflow.com/questions/676949/best-way-to-determine-if-a-url-is-an-image-in-php – donald123 Jul 20 '15 at 12:08
  • thanks @donald123 I'll try the method in the link and be right back –  Jul 20 '15 at 12:12
  • I have placed the function given in the link you sent above the `foreach()` and when I use the function `isImage($search_result['image'])` the page doesn't loads, same thing that happened when I tried the CURL method –  Jul 20 '15 at 12:19
  • @b0s3 thanks for your comment, would you please explain that to me? –  Jul 20 '15 at 12:22

1 Answers1

0

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>';
                }
Domain
  • 11,562
  • 3
  • 23
  • 44
  • Thanks for your answer, but the page is not loading with the `getimagesize()` function, actually none function worked so far inside the `foreach()`... Any ideas? –  Jul 20 '15 at 12:23
  • Could you print_R($search_result); ? – Domain Jul 20 '15 at 12:38
  • yea ofc Array ( [id] => 6 [0] => 6 [account_id] => 2 [1] => 2 [title] => test picture [2] => test picture [location] => some place [3] => some place [world_id] => 0 [4] => 0 [description] => picture description [5] => picture description [image] => http://www.desktopwallpaperhd.net/wallpapers/21/7/wallpaper-sunset-images-back-217159.jpg [6] => http://www.desktopwallpaperhd.net/wallpapers/21/7/wallpaper-sunset-images-back-217159.jpg [date] => 1437117627 [7] => 1437117627 [status] => 1 [8] => 1 [section] => 1 [9] => 1 ) –  Jul 20 '15 at 12:44
  • there are other results in the `print_R` but they are just copies of each other –  Jul 20 '15 at 12:46
  • in your screenshots.php file before including sug.php file just declare $evs_content = ""; – Domain Jul 20 '15 at 12:48
  • Thanks again for your comment, I noticed the lack of it a few minutes ago and tried that but no changes at all –  Jul 20 '15 at 12:55