We know that PHP provides a magic constant __FILE__, which is used to point to the currently executing PHP script. But PHP does not directly provide constants for the directory where the script is located. That is to say, if we want to get the directory where the current PHP script is located, we need to use the dirname() function:
<?php
$dir =dirname(__FILE__);
?>
In PHP5.3, a new constant __DIR__ is added. Points to the directory where the currently executing PHP script is located.
For example, the currently executed PHP file is /www/website/index.php
, then __FILE__ is equal to '/www/website/index.php'
and __DIR__ is equal to '/www/website'.
Now we want to include the current file directory or subdirectory. Files in the directory can be used directly:
<?php
require_once __DIR__ . '/path/to/test.inc.php';
?>