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 some help on this please.

Have you checked that the value of $target_file is actually correct? (E.g., are you sure you want $_SERVER['CONTEXT_DOCUMENT_ROOT'] and not $_SERVER['DOCUMENT_ROOT']?)

if (file_exists($target_file)) {
    echo "Selected file found at $target_file.";
} else {
    echo "Selected file not found at $target_file.";
}

sagecelt Replaced the back-ticks with [code]...[/code] tags in your post to make it easier to read.

Weedpacket
I've tried all the following path definitions:

 $target_file = $_SERVER['DOCUMENT_ROOT'] . '/project/' . $_SESSION["filename"];
 $target_file = 'localhost/project/' . $_SESSION["filename"];
 $target_file = 'C:/wamp64/www/project/' . $_SESSION["filename"];

I've also used 'echo' statements to display $target_file and the total path is always correct as defined in any of the above definitions.
I appreciate your interest and help, thanks.

    NogDog
    Thanks for the tip. I didn't know that worked in this forum; I just used the default from the 'code' icon in the toolbar. 😏

    Just tried to edit one of the above messages but, when I tried to save changes, I got an error message saying I didn't have authority! (Or words to that effect).

      sagecelt
      That's for inline code; I know, it doesn't say anything about that. I'm not thrilled either.

      i too facing the same problem since 3 months and i love it,
      regards,
      Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.

        The function you are using, file_exists, uses physical paths, the parameter you need to provide should be the address on that server ! This is what i think works for u may be

        Ahmed3363
        Yes, you are quite right. Funnily enough I'd worked that out and tested it only a couple of hours ago. 😆
        Nevertheless, I'm grateful for your input, thanks!

          Write a Reply...