0

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.

DoubleA
  • 736
  • 1
  • 7
  • 23
  • 4
    You realise that you're asking your `if` to assess whether the file does ***not*** exist? Therefore you should only get `file exists` when the file does *not* exist...unless I'm really misunderstanding something about the `file_exists()` function. – David Thomas Dec 31 '11 at 22:22
  • @DavidThomas is right. Remove the NOT `!` from the if condition – Ayush Dec 31 '11 at 22:24
  • I agree with @DavidThomas and @ xbonez. The thing is, this also means than neither of your paths work. – Ayman Safadi Dec 31 '11 at 22:29

1 Answers1

1

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";
    }
?>
vdbuilder
  • 12,254
  • 2
  • 25
  • 29
  • I think my problem might be related to if the server does NOT allow to follow symbolic links. Does this make sense? It seems like such a simple issue...but I have wasted days on it. – DoubleA Dec 31 '11 at 23:32
  • @DoubleA can you edit your question to note what part of `$photolocation` is a symbolic link? Also, it would help if you include the output of `getcwd()`. –  Dec 31 '11 at 23:43
  • what os / web server are you running? – vdbuilder Dec 31 '11 at 23:45