Search code examples
phphtmldisplayobject

file_exists() returns false, even for paths that do exist


OK programmers, I'd like to figure this one out before the New Year. I want to display a photo only if it exists, otherwise use a default photo. Here is my code that always correctly returns "File Exists"

<?php 
    $photolocation = '../wp-content/gallery/playerphotos/Joe Smith.jpg';
    if (!file_exists($photolocation))
    {
        echo "File exists";
    }
    else
    {
        echo "File does not exist";
    }
?>

When I change the photolocation to:

$photolocation = '../wp-content/gallery/playerphotos/XXX Smith.jpg';

I incorrectly get "File exists".

I can't figure out why the condition !file_exists always returns a positive value.


Solution

  • That should be:

    <?php 
        $photolocation = '../wp-content/gallery/playerphotos/XXX Smith.jpg';
    
        if (file_exists($photolocation))
        {
            echo "File exists";
        }
        else
        {
            echo "File does not exist";
        }
    ?>