File_exists not working

I’ve read as many previous posts as I can find and tried all the suggested solutions but the following simple test code will not work for me (the session variable has been echoed and is correct). Instead of echoing the correct answer, the if(file_exists($target_file)) skips the true result and always displays the false one. I’ve tested it with files that do and don’t exist of course. Here is the code :-

$filename = $_SESSION["filename"];
$target_file = $_SERVER['CONTEXT_DOCUMENT_ROOT'] . '/folder/' . $_SESSION["filename"];

if (file_exists($target_file)) {
    echo 'Selected file is ' . $filename . ' and will be written.';
} else {
    echo "Selected file " . $filename . " doesn't exist.";
} 

I’d really appreciate any help on this please.

I assume you’ve started the session?

Are you sure CONTEXT_DOCUMENT_ROOT is the correct server variable to use?

Yes, I tried not to bore people with unnecessary code. :slightly_smiling_face:
Actually, that’s about all there is at the moment because I isolated it for testing purposes.

Well, I tried using a hard wired URL and that didn’t work either. This is just on my localhost development machine.

Oh, I also tried $_SERVER[‘DOCUMENT_ROOT’] which was suggested - no joy! :frowning_face:

I have had problems with file_exists() in the past and check with fopen() instead

Now that’s an idea! I’ll give that a whirl tomorrow - winetime now :smile:
Thanks very much.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.