views:

216

answers:

1

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, file_exists() reports that the file does not exist, yet I can still include the same file.

<?php
  $include_path = realpath('path/to/some/directory');
  if(!is_string($include_path) || !is_dir($include_path))
  {
    return false;
  }
  set_include_path(
    implode(PATH_SEPARATOR, array(
      $include_path,
      get_include_path()
    ))
  );
  // 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)".
  var_dump(file_exists($bootstrap));

  // 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 include_once()).

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

Can anyone explain what is causing this behaviour?

Thanks in advance, mniz.

+2  A: 

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.

Matthew Flaschen
Thanks for that, I thought that was the most likely case, but couldn't find any mention of it in the PHP Manual. Thanks for clarifying :)
mynameiszanders