Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require() fails even though file_exists() claims file is there

Tags:

include

php

Here is my code:

if (file_exists('config.php')) {
    require('config.php'); // This is line 38
}

Which somehow produces the error:

Warning: require(config.php) [function.require]: failed to open stream: No such file or directory in /path/name/file.php on line 38

How on earth is this possible?

Update: The following does work:

if (file_exists(getcwd(). '/config.php')) {
     require(getcwd(). '/config.php');
}
like image 638
Mike Avatar asked Dec 07 '22 16:12

Mike


2 Answers

Try absolute path. Using dirname(__FILE__).

require(dirname(__FILE__) . '/config.php');
like image 167
Shiplu Mokaddim Avatar answered Dec 10 '22 12:12

Shiplu Mokaddim


The PHP include and require operate in the same way and use an include path (include). Therefore will use this path to find the file and if you have not set it correctly it will not look in the current directory.

Use get include path to find out the value for this.

like image 42
Ed Heal Avatar answered Dec 10 '22 12:12

Ed Heal