Title: PHP file_exists function won&#039;t work
Last modified: August 21, 2016

---

# PHP file_exists function won't work

 *  [Joontje48](https://wordpress.org/support/users/joontje48/)
 * (@joontje48)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/php-file_exists-function-wont-work/)
 * I have a WordPress website (still running locally) with different pages. I want
   each page to display its own unique header image (using the page name as image
   name), and am able to get that part working smoothly. But, when no header image
   is available (yet) for that specific page, I want to display a placeholder image.
   To do this, I use the file_exists function. It doesn’t work, however. I use this
   code (note: this is a simplified/altered version. I don’t bother you with the
   full code since I don’t even get this simple code to work).
 *     ```
       <img src="<?php
       if (file_exists('/images/headers/home.jpg')) {
           echo '/wp-content/themes/theme/images/headers/home.jpg';
       }
       else {
           echo '/wp-content/themes/theme/images/headers/placeholder.jpg';
       }
       ?>" />
       ```
   
 * The problem is: even though home.jpg exists, it keeps showing me the placeholder.
   jpg.
 * I already tried following file paths:
    – Full link – /wp-content/themes/theme/
   images/headers/home.jpg – /images/headers/home.jpg – home.jpg

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/php-file_exists-function-wont-work/#post-5052534)
 * `file_exists` works best with within the local filesystem and with absolute paths,
   not URLs (at least in general). So you could try something like this:
 *     ```
       if( file_exists( ABSPATH . 'wp-content/themes/theme/images/headers/home.jpg' ) ) {
       }
       ```
   
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/php-file_exists-function-wont-work/#post-5052544)
 * Assuming your code is in the theme’s functions.php file, I’d suggest trying the
   following:
 *     ```
       if (file_exists(dirname(__FILE__) . '/images/headers/home.jpg')) {
       ```
   
 * If you’re using a child theme, this will only work if the image is also part 
   of the child theme.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘PHP file_exists function won't work’ is closed to new replies.

## Tags

 * [exists](https://wordpress.org/support/topic-tag/exists/)
 * [file](https://wordpress.org/support/topic-tag/file/)
 * [file_exists](https://wordpress.org/support/topic-tag/file_exists/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 3 participants
 * Last reply from: [Dion](https://wordpress.org/support/users/diondesigns/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/php-file_exists-function-wont-work/#post-5052544)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
