任何人都可以帮助我获取函数调用的目录的基名?我的意思是:PHP获取文件的名称,其中函数调用
文件/root/system/file_class.php
function find_file($dir, $file) {
$all_file = scandir($dir);
....
}
function does_exist($file) {
$pathinfo = pathinfo($file);
$find = find_file($pathinfo["dirname"], $pathinfo["basename"]);
return $find;
}
文件/root/app/test.php
$is_exist = does_exist("config.php");
在/根/应用程序我有文件“配置。 php,system.php“。你知道如何获得does_exist()
所调用的目录吗?在函数find_file()
自变量$dir
很重要,因为scandir()
函数需要目录路径来扫描。我的意思是,当我想检查文件config.php
我不需要写/root/app/config.php
。如果我没有在$file
参数中提供完整路径,$ pathinfo [“dirname”]将是"."
。我试过在file_find()
函数中使用dirname(__file__)
,但它返回的目录为/root/system
而不是/root/app
,它在does_exist()
函数调用的目录中。
我需要创建这些功能,因为我不能使用file_exists()
函数。
找到的解决办法:
我使用debug_backtrace()
获得,用户调用函数的最近的文件和行号。例如:
function read_text($file = "") {
if (!$file) {
$last_debug = next(debug_backtrace());
echo "Unable to call 'read_text()' in ".$last_debug['file']." at line ".$last_debug['line'].".";
}
}
/home/index.php
16 $text = read_text();
示例输出:Unable to call 'read_text()' in /home/index.php at line 16.
感谢。