start.php
<?php
if(file_exists('/config.php')) require_once('/config.php');
echo TEST;
?>
config.php
<?php
define('TEST','Hamsters');
?>
I have Windows XP + XAMPP with PHP Version 5.3.8.
If I run start.php it gives me this error:
Notice: Use of undefined constant TEST - assumed 'TEST' in C:\programs\xampp\htdocs\start.php on line 3
Now I modify start.php to the following and he gives me my Hamsters
:
<?php
require_once('/config.php');
echo TEST;
?>
How can file_exists()
say the file not exist but without the condition still be able to require_once()
the file that claimed non-existent?
The problem was that /config.php
actually means C:/config.php
.
File_exists() only checks the actual file or folder if it is exist and if not it will give a false
.
Require_once() does some more. According to the PHP manual this function is almost identical to require() that is almost identical to include(). The non identical parts are not important in our case. What is important, that the manual says about include:
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
Because config.php
was in the same directory where start.php
was, after require_once()
found out that config.php
is not in C:/
it searched and found it in the directory where start.php
called him. File_exists()
does not do this search thus it returns false and require_once()
can't be called.
If I copy my config.php
file to C:/
it will work with the file_exists()
way too.