Get absolute path of initially run script
echo realpath(dirname(__FILE__));
If you place this in an included file, it prints the path to this include. To get the path of the parent script, replace __FILE__
with $_SERVER['PHP_SELF']
. But be aware that PHP_SELF is a security risk!
__FILE__
constant will give you absolute path to current file.
Update:
The question was changed to ask how to retrieve the initially executed script instead of the currently running script. The only (??) reliable way to do that is to use the debug_backtrace
function.
$stack = debug_backtrace();
$firstFrame = $stack[count($stack) - 1];
$initialFile = $firstFrame['file'];