現在のスクリプトの絶対パスを取得

絶対パスを取得するための情報を含む様々なソリューションや変数を探しました。 しかし、それらはある条件下では機能しますが、別の条件下では機能しないようです。 PHPで実行されたスクリプトの絶対パスを取得する方法はありますか? 私の場合、スクリプトはコマンドラインから実行されますが、解決策はApacheなどの中で実行されても同じように機能するはずです。

**明確にします。 最初に実行されたスクリプトであり、必ずしもソリューションがコード化されたファイルではありません。

定数__FILE__は、現在のファイルへの絶対パスを与えます。

Update:

質問は、現在実行中のスクリプトではなく、最初に実行されたスクリプトを取得する方法を尋ねるものに変更されました。 これを行う唯一の(??)信頼できる方法は、debug_backtrace関数を使用することです。

$stack = debug_backtrace();
$firstFrame = $stack[count($stack) - 1];
$initialFile = $firstFrame['file'];
解説 (16)

の例です。 *https://(www.)example.com/subFolder/myfile.php?var=blabla#555**。

// ======= PATHINFO ====== //
$x = pathinfo($url);
$x['dirname']      🡺 https://example.com/subFolder
$x['basename']     🡺                               myfile.php?
$x['extension']    🡺                                      php?k=blaa#12345 // Unsecure! also, read my notice about hashtag parts    
$x['filename']     🡺                               myfile

// ======= PARSE_URL ====== //
$x = parse_url($url);
$x['scheme']       🡺 https
$x['host']         🡺         example.com
$x['path']         🡺                    /subFolder/myfile.php
$x['query']        🡺                                          k=blaa
$x['fragment']     🡺                                                 12345 // ! read my notice about hashtag parts

//=================================================== //
//========== self-defined SERVER variables ========== //
//=================================================== //
$_SERVER["DOCUMENT_ROOT"]  🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"]    🡺 143.34.112.23
$_SERVER["SERVER_PORT"]    🡺 80(or 443 etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https                                         //similar: $_SERVER["SERVER_PROTOCOL"] 
$_SERVER['HTTP_HOST']      🡺         example.com (or with WWW)             //similar: $_SERVER["ERVER_NAME"]
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?k=blaa
$_SERVER["QUERY_STRING"]   🡺                                             k=blaa
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php
__DIR__                    🡺 /home/user/public_html/subFolder              //same: dirname(__FILE__)
$_SERVER["REQUEST_URI"]    🡺                       /subFolder/myfile.php?k=blaa
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺  /subFolder/myfile.php 
$_SERVER["PHP_SELF"]       🡺                       /subFolder/myfile.php

// ==================================================================//
//if "myfile.php" is included in "PARENTFILE.php" , and you visit  "PARENTFILE.PHP?abc":
$_SERVER["SCRIPT_FILENAME"]🡺 /home/user/public_html/parentfile.php
$_SERVER["PHP_SELF"]       🡺                       /parentfile.php
$_SERVER["REQUEST_URI"]    🡺                       /parentfile.php?abc
__FILE__                   🡺 /home/user/public_html/subFolder/myfile.php

// =================================================== //
// ================= handy variables ================= //
// =================================================== //
//If site uses HTTPS:
$HTTP_or_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' );            //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])  ...

//To trim values to filename, i.e. 
basename($url)             🡺 myfile.php

//excellent solution to find origin
$debug_files = debug_backtrace();       
$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1]['file'] : __FILE__;

お知らせです!。

  • ハッシュタグ(#...)のURL部分は、PHP(サーバーサイド)では検出できません。 そのため、JavaScript を使用します。
  • DIRECTORY_SEPARATOR は、Windows タイプのホスティングでは / の代わりに \ を返します。

となります。


WordPressの場合

//(let's say, if wordpress is installed in subdirectory:  http://example.com/wpdir/)
home_url()                      🡺 http://example.com/wpdir/        //if is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/  
解説 (2)
echo realpath(dirname(__FILE__));

これをインクルードされたファイルに置くと、このインクルードへのパスが表示されます。親スクリプトのパスを取得するには、__FILE__$_SERVER['PHP_SELF'] に置き換えます。しかし、PHP_SELFはセキュリティ上のリスクがあることに注意してください!

解説 (12)