定義がサブディレクトリにあるときに実際の (完全な) ルートパスを取得する信頼できる方法は? 検討:
E:\web\sites\name\index.php
E:\web\sites\name\res\defines.php
E:\web\sites \name\res\another.php E:\web
\sites\name\classes \class-example.php
E:\web\
localhostはどこE:\web\sites\name
にあり、サイトの意図されたルートです。
E:\web\sites\name\data\feature\file.txt
=を次のように定義するパスの例$file = ROOTPATH."\data\feature\file.txt";
相対パスを使用しない理由は、同じターゲットに対して 2 つの定数を使用せずclasses\class-example.php
にパスを渡したいからです。res\another.php
では、res\defines.php で、rootpath を指すようにするにはどうすればよいE:\web\sites\name\
でしょうか? Web サイトがサーバー上にある場合、同じ ROOTPATH がhttp://www.example.com/
define.php が にあるときに得られるものは次のとおりですres\defines.php
。
// Outputs: E:\web\sites\name\res
echo __DIR__;
// Outputs: E:\web\sites\name\res
echo realpath(__DIR__);
// Outputs: sites\name\res\another.php
echo $_SERVER['REQUEST_URI']
// Outputs: E:\web
echo $_SERVER['DOCUMENT_ROOT'];
// Outputs: sites\name\res\another.php
echo $_SERVER['SCRIPT_NAME'];
// Outputs: E:\web\sites\name\res\defines.php
echo __FILE__;
// Outputs: sites\name\res\another.php
echo $_SERVER['PHP_SELF'];