Jump to content

File_Exists Returns False Even Though the File is There


chaseman

Recommended Posts

I'm working on a WordPress theme and I'm trying to build in a simple if statement which will check if the user has add his own logo into the images folder if he doesn't then the name of the blog will appear as normal text in place of the graphic logo.

 

This is how it looks like:

 

<?php
$logo_dir = get_template_directory_uri() . "/images/logo.png";

if (file_exists($logo_dir)) { ?>

	<li><img src="<?php bloginfo ('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('description'); ?>" /></li>

<?php } else { ?>

	<li id='blog_name'><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?><font>*</font></a></li>

<?php } ?>


 

I've echo'd out $logo_dir, the URL is correct, but still for some reason it's seeing it as if there would no file exist. If I turn it around into !file_exists then the graphic logo WILL show up. So it's always seeing it as non-existent.

 

Any ideas why this could be or am I using this function in a wrong fashion? As far as I've understood the PHP manual, this is the correct way of using it.

Link to comment
Share on other sites

^^What he said ...

 

and you are not using the same function call to get the name to test as you are to get the name to display

 

$logo_dir = get_template_directory_uri() . "/images/logo.png";
if (file_exists($logo_dir)) { ?>
<li><img src="<?php bloginfo ('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('description'); ?>" /></li>

 

I don't know the WordPress API, but I would guess that get_template_directory_uri() is going to return a value suitable for sending to the browser; and that bloginfo('template_directory') is going to return a file path suitable for use in PHP. Check for the proper usage of those functions.

Link to comment
Share on other sites

Thanks for the responses.

 

To David,

 

bloginfo will return the full http URL as well, on top of it will even echo it out, which won't enable me to put it into a variable since then I'd get the URL displayed on the page, that's why I used the other function which doesn't have an echo.

 

I think I could just add the relative URL myself e.g. ('images/logo.png'), I don't how much problems it could cause when other user's are using the template?

 

I'll see.

 

 

EDIT: I just tried adding the relative URL by hand like this:

 

$logo_dir = "/images/logo.png";

 

But this does not work either.

 

When I put this php file into my themes folder it will work then:

 

<?php 

$logo = "images/logo.png";

if (file_exists($logo)) {

echo "works"; 

}


?>

 

For some reason it just doesn't work inside the Wordpress header.php file, it could be a wordpress specific thing. They could have disabled or modified it or something like that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.