Reputation: 76240
This is the code I'm using (it's the index.php file)
/* Set an include path
*/
$dir = explode('/', __DIR__);
$i = count($dir);
unset($dir[$i-1], $dir[$i-2]);
$path = implode('/', $dir);
ini_set('include_path', $path);
// require('system/base/file.php'); // ***1
/* Starter file
*/
if (file_exists('system/base/file.php')) { require('system/base/file.php'); }
else { exit('Error'); }
I'm developing a framework that has this structure
application/
public/
index.php
system/
And I want to set the include path to the root/ (the folder that contains application/ and system/). __DIR__
is giving me a lot of things suchs as Application/xammp/htdocs/application/public/index.php
(in localhost); I'm not really sure that clients __DIR__
is so much different, considering that I don't know nothing about the path in the first place. I just wrote those first few line to easily delete the last 2 folders from DIR so I'm sure that the path is right whatever __DIR__
is. I tested those lines in another test setting and they are working fine.
The strange thing that happens is that if I run the the code as it is shown up there it gives me "Error". While if I just require system/base/file.php
before checking it's existence it works. So if I uncomment (***1) the file is required.
Upvotes: 1
Views: 134
Reputation: 8073
You could use $path = dirname(dirname(DIR)), this will achieve the same thing that your first 5 lines of codes do.
According to the PHP documentation, include_path only works with: require(), include(), fopen(), file(), readfile() and file_get_contents(), not file_exists(). Therefore, you will have to store $path in a variable or a constant. For example:
/* Set an include path
*/
$path = dirname(dirname(__DIR__));
ini_set('include_path', $path);
define('PATH', $path)
// require('system/base/file.php'); // ***1
/* Starter file
*/
if (file_exists(PATH . 'system/base/file.php')) { require('system/base/file.php'); }
else { exit('Error'); }
Upvotes: 2