When running PHP on the command line, if you want to include another file which is in the same directory as the main script, doing just
<?php
include './otherfile.php';
?>
might not work, if you run your script like this:
/$ /path/to/script.php
because the current working dir will be set to '/', and the file '/otherfile.php' does not exist, because it is in '/path/to/otherfile.php'.
So, to get the directory in which the script resides, you can use this function:
<?php
function get_file_dir() {
global $argv;
$dir = dirname(getcwd() . '/' . $argv[0]);
$curDir = getcwd();
chdir($dir);
$dir = getcwd();
chdir($curDir);
return $dir;
}
?>
So you can use it like this:
<?php
include get_file_dir() . '/otherfile.php';
chdir(get_file_dir());
include './otherfile.php';
?>
Spent some time thinking this one out, maybe it helps someone :)