mardi 27 novembre 2018

PHP - How to check if URL (for an external image file) exists when the extension is unknown

I have a loop parsing XML data for an RSS feed and I need a way to check if an external image file exists (which is unknown), and if so, display it.

The problem is, the image file may end in JPG or PNG, and may change each time, so I can't just check for the file directly.

Is there a way to run a loop to check for that?

This is what I have so far... but I don't know how to add a condition to check for .jpg or .png as part of $unknown_file_extension

$items = array_slice($feed->items, 0, 9999); // show all

    if (!empty($items)) :
        foreach ($items as $item) :

        $image_file = 'http://somedomain/'.$known_path.'/'.$known_filename.'/'.$unknown_file_extension;

        $file_headers = @get_headers($image_file);
          if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {    
           // exists = false, do nothing
          }
          else {
           // exists = true, display image
          }

        endforeach;
    endif;

Aucun commentaire:

Enregistrer un commentaire