I'm working on a php application that uses a common name format for all images (image-$image_id.jpg), because of this it references broken images someway, is there a function that does something like this:

$image = "image-$image_id.jpg";

if(!$image) {

$image = "no_image.jpg";

}

I know this code is totally bogus but I hope you see what I'm trying to do. I have an image on the server no_image.jpg and I want a function to check if the image that $image is referencing is a valid file, if it isn't, to change it to reference a different file (no_file.jpg).

A better example might be something like this:

if(!fileexists("image-$image_id.jpg")) {

$image = "no_image.jpg";

}

Thanks,
Ben

    There is a php function called file_exists("file_path/name.jpg") that returns true / false

      <?
      $image = "image-$image_id.jpg";
      if(!file_exists($image)) { 
         $image = "no_image.jpg"; 
      }
      print $image;
      

        hand's code will print the no-image.jpg if $image is not valid and also print the $image no mater what. I think what your looking for is if $image is not valid, use no-image.jpg, else if it is valid use $image.jpg. If so give this a try.

        <?php
        $image = "image-".$image_id.".jpg";
        if (file_exists($image)) {
            echo $image;
        }
        else {
            echo "no_image.jpg";
        }
        ?>
        

          @
          errare humanum est

          My script works in both cases. You don't believe it?

            @
            Ooopss...I didn't look at your script close enough. I though you printed the no-image if the file didn't exsist. Sorry about that.

            @everyone
            In correction to my post; hand's script will work just the same as mine would. You can use either or, hands is just a bit shorter.

              😃
              You're welcome, but it was not neccessary
              😃

                Write a Reply...