Hey.

I have a possible 4 different images with the same file name but different extention (.png, .jpeg, .jpg, .gif) - Only 1 can exist at a time. How would I go about checking if each image exists then displaying the one image that exists? If none exist then it displays some text.

If I am not clear with what I need help with, please say!

Many thanks!

    Hi,

    You could have an array that loops through the extensions ...

    // given $path_to_image (excluding the extension)
    $exts = array('png', 'jpeg', 'jpg', 'gif');
    $file = '';
    foreach($exts as $ext){
      if(file_exists($path_to_image.'.'.$ext)){
        $file = $path_to_image.'.'.$ext;
        break;
      }
    }
    if($file){
    // image
    } else {
    // text
    }

    Look up the function file_exists() in the manual to get the technicalities of paths.

    P.

      Thanks for the reply!

      I have edited the code

      		// given $path_to_image (excluding the extension)
      $exts = array('png', 'jpeg', 'jpg', 'gif');
      $file = '';
      $path_to_image = 'http://site.com/community/avatars/'.strtolower($current_user->
          user_login);
      foreach($exts as $ext){
        if(file_exists($path_to_image.'.'.$ext)){
          $file = $path_to_image.'.'.$ext;
          break;
        }
      }
      if($file){
       echo '<img src="'.$file.'" width="20px" height="20px" />';
      } else {
      echo 'Hello';
      } 

      And even thought there is a file called perry.jpg in the specified folder it takes a while to process the script then just displays 'Hello'

      Why is this happening?

      Regards.

        Did you look up the file_exists() function? There will be rules about how you specify the path - I never remember - I just look them up. The user comments at the bottom of each page in the manual usually have just what you're after.

        P

          http://site.com/community/avatars/

          It's probably taking a while because it's going to the trouble of attempting to download the image from site.com. If it's supposed to be looking on your server, just use a file path instead of an HTTP URL.

            Thank you for your help. I have solved my problem! For future reference to users:

            		// given $path_to_image (excluding the extension)
            $exts = array('png', 'jpeg', 'jpg', 'gif');
            $file = '';
            $path_to_image = '/home/path/to/folder/community/avatars/'.strtolower($current_user->user_login);
            
            foreach($exts as $ext){
              if(file_exists($path_to_image.'.'.$ext)){
                $file = $path_to_image.'.'.$ext;
                break;
              }
            }
            if($file){
            	$type = substr(strrchr($file, "."), 1);
             echo '<img src="http://site.com/community/avatars/'.strtolower($current_user->user_login).'.'.$type.'" width="20px" height="20px" />';
            } else {
            echo '<img src="http://site.com/community/avatars/default.png" width="20px" height="20px" />';
            }
              Write a Reply...