Get absolute path of current script

I have searched high and low and get a lot of different solutions and varialbles containing info to get the absolute path. But they seem to work under some conditions and not under others. Is there one silver bullet way to get the absolute path to the current executing script in php? For me the script will be running from the command line but it should just as well function if run within apache etc.

Clarification: The initial executed script, not the file we are currently in



__FILE__ constant will give you absolute path to current file.

UPD :

As soon as question was changed to retrieve the script that initiated runtime 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'];

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!

链接地址: http://www.djcxy.com/p/9732.html

上一篇: Echo换行符在Bash中打印文字\ n

下一篇: 获取当前脚本的绝对路径