Hi, I'm trying to use php to check if a file exists and then display some information on the page. Here are some scenarios and the results: Scenario 1: $number = 101; $dir = "/full/path/name"; $fakeFile = "$dir/tmp/doesnotexist.log"; $realFile = "$dir/tmp/doesexist.split"; [COLOR="Red"]$fileExists = file_exists($realFile);[/COLOR] if(!file_exists("$dir/aFileThatDefinitelyDoesNotExist.log")) {[COLOR="Red"][INDENT]echo $number;[/INDENT][/COLOR]} //Output: 101 Code (markup): [U]Scenario 2:[/U] $number = 101; $dir = "/full/path/name"; $fakeFile = "$dir/tmp/doesnotexist.log"; $realFile = "$dir/tmp/doesexist.split"; [COLOR="Red"]$fileExists = file_exists($realFile);[/COLOR] if(!file_exists("$dir/aFileThatDefinitelyDoesNotExist.log")) {[COLOR="Red"][INDENT]echo $fileExists;[/INDENT][/COLOR]} //Output: 1 Code (markup): Scenario 3: $number = 101; $dir = "/full/path/name"; $fakeFile = "$dir/tmp/doesnotexist.log"; $realFile = "$dir/tmp/doesexist.split"; [COLOR="Red"]$fileExists = file_exists($fakeFile);[/COLOR] if(!file_exists("$dir/aFileThatDefinitelyDoesNotExist.log")) {[COLOR="Red"][INDENT]echo $number;[/INDENT][/COLOR]} //403 Forbidden error, page does not load. Code (markup): Scenario 4: $number = 101; $dir = "/full/path/name"; $fakeFile = "$dir/tmp/doesnotexist.log"; $realFile = "$dir/tmp/doesexist.split"; [COLOR="Red"]$fileExists = file_exists($fakeFile);[/COLOR] if(!file_exists("$dir/aFileThatDefinitelyDoesNotExist.log")) {[COLOR="Red"][INDENT]echo $fileExists;[/INDENT][/COLOR]} //Blank output Code (markup): This is wrong on soooo many levels. file_exists() is supposed to return boolean, not 1. $fakeFile definitely does not exist and $realFile definitely does exist and all paths are correct, barring any typos I may have made in copying the code over. This should be a really simple piece of work but there's obviously something I'm missing. Please help! Thanks.
Sorry, that was what I meant to type. It's fixed in the original post now but the errors are the same. From what I can tell by doing other tests file_exists() seems to be returning "1" when the file exists and "" when it doesn't (no quotes though).
I tested Scenario 2 returns blank and Scenario 3 returns 101. Do you have something like memcache or wincache extensions installed with your PHP? I a not getting same result as you. Can you make a new file and put all 4 scenarios and try "var_dump()" instead of echo?
Getting new results now... interesting. Here is my program I wrote to test (besides html, body tags): <?php $dir = "/full/path/name"; $realFile = "$dir/tmp/doesexist.split"; $fakeFile = "$dir/tmp/doesnotexist.log"; $number = 101; $SCENARIO = 5; //change this to test different scenarios if ($SCENARIO == 1){ $fileExists = file_exists($realFile); //if(file_exists("$dir/tmp/aFileThatDefinitelyDoesExist.log")) { var_dump($number); //} } elseif ($SCENARIO == 2){ $fileExists = file_exists($realFile); // if(file_exists("$dir/tmp/aFileThatDefinitelyDoesExist.log")) { var_dump($fileExists); // } } elseif ($SCENARIO == 3){ $fileExists = file_exists($fakeFile); //if(file_exists("$dir/tmp/aFileThatDefinitelyDoesExist.log")) { var_dump($number); //} } elseif ($SCENARIO == 4){ $fileExists = file_exists($fakeFile); //if(file_exists("$dir/tmp/aFileThatDefinitelyDoesExist.log")) { var_dump($fileExists); //} } else { echo "Invalid scenario $SCENARIO"; } ?> PHP: Note that the ifs are commented because I was making sure it wasn't them that was the problem. Output: S1: int(101) =>sometimes 403 Forbidden, no idea why it changes! S2: 403 Forbidden S3: 403 Forbidden S4: bool(false) =>sometimes 403 Forbidden, no idea why it changes! S5: Invalid scenario 5 =>sometimes 403 Forbidden, no idea why it changes! ?????
can you try to use for loop to loop from scenario 1 to 4 so you get all results in 1 page? also try chmod 777 your file if on linux, or set user's permission in windows so your web server can access the file. Sounds like the user permission for the file changed whenever you saved the file
Wow that worked pretty well. Here's the output: 1int(101) 2bool(true) 3int(101) 4bool(false) Code (markup): All on one line for some reason but giving the expected output values which is good. Ok, so why in the context of my program is this returning 1 or blank? Edit: Sorry the numbers in front are the scenario numbers which I had it print just to keep track. Forgot to mention that.
The outputs are all on one line because there isn't anything like <br /> tag to separate them. You can "view source" on your browser or wrap your code witih <pre></pre> tags to see the formatted version. As for your programming returning 1 or blank. That's just how echoing works. "echo true" returns 1, "echo false" returns blank. "var_dump()" gives you more details information. Cheers, Felix