6

I have the following code snippet:

<?
include("/classes/functions.php");
if(!file_exists("/config.ini")) redirect("/classes/install.php");
else{
    //file is processed
}
?>

What this is supposed to do is to read data from a configuration file then use it to connect to a MySQL server. If the configuration file doesn't exist, it redirects to a setup page where the file is created and filled with user-provided data.

Problem is, even though the file doesn't exist, file_exists returns true anyway, which causes the else branch to run and fail all over the place.

I tried using $_SERVER['DOCUMENT_ROOT'] in the file path, just in case; no difference.

5
  • Maybe you can use the full path to the file, like /home/user/domains/htdocs/ Commented Jul 26, 2016 at 18:48
  • If you use a IDE which can debug with breakpoints, try set one right before and step through the code. Commented Jul 26, 2016 at 18:54
  • I would use __DIR__ instead of server. Commented Jul 26, 2016 at 18:57
  • This code runs right when the page is displayed. I found a processing problem in an echo command a few lines below, still in the else branch: if the server couldn't be reached, a yes/no popup is displayed and the page is redirected on an affirmative. However, the echo command that generates this JS popup breaks and belches out everything after the opening JS script tag's > as plaintext. If this script is allowed to run as is, the redirection is successful - but the redirection in the if branch does NOT work despite using the exact same code. So the problem is definitely file_exists. Commented Jul 26, 2016 at 19:17
  • I think I figured out what's wrong. I put echos in both the if and the else branch to see which way the script is going. Neither ran. Which means that the script is not running whatsoever. Commented Jul 26, 2016 at 19:33

3 Answers 3

9

Documentation: http://php.net/manual/en/function.file-exists.php

Returns TRUE if the file or directory specified by filename exists;

file_exists('/non-existing-file.ini') returns true because path '/' exists

use is_file() instead

Sign up to request clarification or add additional context in comments.

2 Comments

that's incorrect, running if(file_exists("folder/file.php")) { return true;} will return true if folder/file.php is there but if(file_exists("folder/WRONG.php")) { return true;} does not return true. Eventhough "folder" IS there. Just ran this short test on my side. Unless i'm missing something.. Could be a clearstatcache() issue since file_exists caches results
indeed, it seems I got fooled by the cache. Anyway, I recommend is_file because generated file name can be empty and file_exists is tricky in this case: file_exists('/path/to/'.generated())
0

Found the source of the problem. For whoever might stumble upon this in the future, the Apache that comes with XAMPP doesn't execute PHP code if it's prefixed with <? instead of <?php.

Comments

-1

the probleme is in the redirect() function . there is no redirect in php use this code

<?
include("/classes/functions.php");
if(!file_exists("/config.ini")){
 header("Location: /classes/install.php");
}else{
    //file is processed
}
?>

2 Comments

could have a function named that, and you have a quote issue on header
I indeed have a redirect() function, found in the included file. It merely generates a JS window.location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.