Fogolicious
Fogolicious

Reputation: 412

Checking if a image exists

I want to check if the file exists before I use it on the page. From what I understand something like this should work but nothing appears so I am assuming I have not coded it correctly.

<?php 
    $url = get_bloginfo('template_directory');
    $page_id = get_queried_object_id();    

        if(file_exists( $url . '/images/footerImage' . $page_id . '.png')) { ?>
            <img class="footerImage" src="<?php echo get_bloginfo('template_directory'); ?>/images/footerImage<?php echo $page_id; ?>.png" />
        <?php } ?>

I want it to check to see if the file exists and if it does create the image tag, if not do nothing. Why is this not showing any images? I know I have the images because it was working before I had the if statement but creating a broken image on a page that I did not have an image for.

Upvotes: 0

Views: 937

Answers (1)

Krish R
Krish R

Reputation: 22711

get_bloginfo('template_directory') function returns the Absolute URL. But you need to supply the Relative path in order to check the file existence file_exists()

if(file_exists( TEMPLATEPATH . '/images/footerImage' . $page_id . '.png')) { ?>
        <img class="footerImage" src="<?php echo get_bloginfo('template_directory'); ?>/images/footerImage<?php echo $page_id; ?>.png" />
<?php } ?>

Upvotes: 2

Related Questions