file_exists() causing page to crash

Discussion in 'PHP' started by Theruchet, Jul 16, 2010.

  1. #1
    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.
     
    Last edited: Jul 16, 2010
    Theruchet, Jul 16, 2010 IP
  2. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this

    
    if(!file_exists("$dir/aFileThatDefinitelyDoesNotExist.log")) {
    
    Code (markup):
     
    imperialDirectory, Jul 16, 2010 IP
  3. Theruchet

    Theruchet Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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).
     
    Last edited: Jul 16, 2010
    Theruchet, Jul 16, 2010 IP
  4. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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?
     
    imperialDirectory, Jul 16, 2010 IP
  5. Theruchet

    Theruchet Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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!

    ?????
     
    Theruchet, Jul 16, 2010 IP
  6. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    imperialDirectory, Jul 16, 2010 IP
  7. Theruchet

    Theruchet Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    Theruchet, Jul 16, 2010 IP
  8. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    imperialDirectory, Jul 16, 2010 IP
  9. Theruchet

    Theruchet Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ooooooooooooooh awesome! Thanks for your help! :D
     
    Theruchet, Jul 16, 2010 IP