mynameiszanders mynameiszanders - 1 year ago 80
PHP Question

PHP: Can include a file that file_exists() says doesn't exist

In my script, I set the include path (so another part of the application can include files too), check that a file exists, and include it.

However, after I set the include path,

reports that the file does not exist, yet I can still include the same file.

$include_path = realpath('path/to/some/directory');
if(!is_string($include_path) || !is_dir($include_path))
return false;
implode(PATH_SEPARATOR, array(
// Bootstrap file is located at: "path/to/some/directory/bootstrap.php".
$bootstrap = 'bootstrap.php';

// Returns "bool(true)".
var_dump(file_exists($include_path . '/' . $bootstrap));
// Returns "bool(false)".

// This led me to believe that the include path was not being set properly.
// But it is. The next thing is what puzzles me.

require_once $bootstrap;
// Not only are there no errors, but the file is included successfully!

I can edit the include path and include files without providing the absolute filepath, but I cannot check whether they exist or not. This is really annoying as every time a file that does not exist is called, my application results in a fatal error, or at best a warning (using

Turning errors and warnings off is not an option, unfortunately.

Can anyone explain what is causing this behaviour?

Answer Source

file_exists does nothing more than say whether a file exists (and the script is allowed to know it exists), resolving the path relative to the cwd. It does not care about the include path.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download