関数が呼び出されたディレクトリのベース名を取得するのを手伝ってくれる人はいますか? つまり:
ファイル /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");
/root/app の下に、「config.php、system.php」というファイルがあります。does_exist()
呼び出されたディレクトリを取得する方法を知っていますか? 関数にはスキャンするディレクトリ パスが必要なため、関数内のfind_file()
引数$dir
は重要です。scandir()
つまり、ファイルをチェックしたいときは、config.php
書く必要はありません/root/app/config.php
。引数にフルパスを指定しない場合$file
、$pathinfo["dirname"] は"."
. dirname(__file__)
関数で使用しようとしましたが、呼び出された関数のディレクトリではなくfile_find()
、ディレクトリを返します。/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.
ありがとう。