Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Problem with if (file_exists) with dynamic image WordPress

  • SOLVED

I am working on this code to display a taxonomy image, but only returns false, even when the image exists. Below is a simplified version of my code without full file paths:

<?php
$tax_image = get_query_var( 'term' );
$filename = '/images/artists/'.$tax_image.'.jpg' ;
if (file_exists($filename)) {
echo '<img src="/images/artists/'.$tax_image.'.jpg" />';
} else {
echo 'Does not exist';
}
?>


I've tested it by replacing <em>'Does not exist'</em> with<em><img src="/images/artists/'.$tax_image.'.jpg" /></em> and the image does appear. So it appears there's a problem with this line <em>$filename = '/images/artists/'.$tax_image.'.jpg' ;</em>.

Thanks for your answers!

Answers (3)

2011-09-24

Grégory Viguier answers:

Hi there.

get_template_directory_uri() returns a uri, not a path, file_exists() needs a path.
If your images folder is in your theme, you can do that :
$filename = WP_CONTENT_DIR . '/your-theme/images/artists/'.$tax_image.'.jpg' ;

If you're creating a plugin, use this instead (I mean, your images folder is inside your plugin) :
DEFINE( 'MY_PLUGIN_DIR', trailingslashit( WP_PLUGIN_DIR ) . basename( dirname( __FILE__ ) ) );
$filename = MY_PLUGIN_DIR . /images/artists/'.$tax_image.'.jpg' ;


EDIT : and of course you can still use this for the image uri :
<img src='".get_template_directory_uri()."/images/artists/".$tax_image.".jpg' />


Jeremy Phillips comments:

Thanks, I've tried inserting <strong>WP_CONTENT_DIR</strong> as well as carefully typing out the full url in the <strong>$filename =</strong> line, but it is still returning false.


Grégory Viguier comments:

Indeed, it's very strange.

By the way, my mistake, I forgot the "/themes" folder (I always do, may be you too?) :
$filename = WP_CONTENT_DIR . '/themes/your-theme/images/artists/' . $tax_image . '.jpg';

Did you "echo" this $filename to see if there's something wrong somewhere? (I assume you did)


Jeremy Phillips comments:

When I echo file name I get this:
http://my-domain.com/wp-content/themes/my-theme/images/artists/'.my-file-name.'.jpg/


Grégory Viguier comments:

As I said, file_exists() won't work with a url (http://...), try again with:
$filename = WP_CONTENT_DIR . '/themes/your-theme/images/artists/' . $tax_image . '.jpg';
I think it's just a little mistake in the string (in my case, I always forget the /themes folder before /my-theme).

PS: it's WP_CONTENT_DIR, not WP_CONTENT_URL, it's not the same thing.


Jeremy Phillips comments:

Sorry, I tried before as well. Below is what I got when I echoed $filename (and still returned false):

/home/content/47/6912147/html/my-domain/wordpress/wp-content/themes/my-theme/images/artists/my-file-name.jpg/


Grégory Viguier comments:

Well, it was :
$filename = WP_CONTENT_DIR . '/themes/my-theme/images/artists/' . $tax_image . '.jpg';
and not :
$filename = WP_CONTENT_DIR . '/themes/my-theme/images/artists/' . $tax_image . '.jpg/';
(slash at the end).

2011-09-24

Pippin Williamson answers:

You need to use an absolute path. So, if you are doing this in your theme, and the "images" folder is inside of your theme directory, the use this:

$filename = get_template_directory_uri() . '/images/artists/'.$tax_image.'.jpg' ;

So final code would be:


<?php
$tax_image = get_query_var( 'term' );
$filename = get_template_directory_uri() . '/images/artists/'.$tax_image.'.jpg' ;
if (file_exists($filename)) {
echo '<img src="/images/artists/'.$tax_image.'.jpg" />';
} else {
echo 'Does not exist';
}
?>


Jeremy Phillips comments:

Thanks, Pippin. I was using absolute file paths in all instances, I just edited them for the post.

I just threw in the template directories and here is the entire unedited code:
<?php
$tax_image = get_query_var( 'term' );
$filename = get_template_directory_uri() . '/images/artists/'.$tax_image.'.jpg' ;
if (file_exists($filename)) {
echo "<a href='".get_template_directory_uri()."/images/artists/".$tax_image.".jpg' rel='prettyPhoto'/>";
echo "<img src='".get_template_directory_uri()."/images/artists/".$tax_image.".jpg' />";
echo '</a>';
} else {
echo 'Does not exist';
}
?>


Like I mentioned, it returns false in every instance. When I replace 'Does not exist' with "<img src='".get_template_directory_uri()."/images/artists/".$tax_image.".jpg' />" the image does appear. Seems like this code should work...

2011-09-25

ej_emman answers:

Hello Jeremy,

Ahm.. You have said, When you echo the file you have this:
<blockquote>http://my-domain.com/wp-content/themes/my-theme/images/artists/'.my-file-name.'.jpg</blockquote>
Ahm.. Can you place this to the URL address? and see what happen..

If there's an error, try to set permission to this file. Making it readable...

Hope this help!

Cheers,