开发者

How to get the absolute path of the calling file in a function that's called in PHP?

开发者 https://www.devze.com 2022-12-22 16:30 出处:网络
in file.php: call()... How to get absolute path of f开发者_开发百科ile.php inside call() ? Pay attention that call() may be defined in another file.

in file.php:

call()...

How to get absolute path of f开发者_开发百科ile.php inside call() ?

Pay attention that call() may be defined in another file.

another.php:

function call(){..}

file.php:

include(path_to_another.php);
call();


no way except debug_backtrace

function whatever() {
    $t = debug_backtrace();
    echo "called from {$t[0]['file']}";
}


In addition to James Brooks's answer and Bretticus observation, if you need to get the basename of the calling file you can do:

$backtrace = debug_backtrace();
$file_name = basename($backtrace[0]['file']);


You can also get the basename for the file, like so;

basename(__FILE__);

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消