1 Place the following code in a file in the root directory of the Web site so that it can be referenced in other files. 2 3 path mode:4 5 Define(' Base_path ',Str_replace(‘\\‘,‘/‘,Realpath(dirname(__file__).‘ /‘))." /");6 7 EchoBase_path;8 9 Ten OneOutput Result: e:/wwwroot/ A - URL Method: - the - - $PHP _self=$_server[' Php_self ']; - + $url= ' http://'.$_server[' Http_host '].substr($PHP _self, 0,Strrpos($PHP _self, '/') +1); - + Echo $url; A at - -Output Result: http://localhost/ - other information to organize: - php Gets the site root directory - in - to Echo $_server[' Http_host '];//Host of the current request: the content of the header is the domain name letter information + - Echo $_server[' Php_self '];//The file that is currently executing the script relative to the Web site root address, even if the file is referenced by another file to get the address correctly the * $ Panax Notoginseng Echo $_server[' Script_name '];//The file that is currently executing the script is relative to the site root address, but when the file is referenced by another file, only the relative address of the referenced file is displayed and the relative address of the referenced script is not displayed. - the + A Echo $_server[' Document_root '];//the site relative to the server address is the absolute path name of the site #当前运行脚本所在的文档根目录. Defined in the server configuration file the + - $ Echo $_server[' Script_filename '];//the absolute path name of the currently executing script. $ - - the method One: - Wuyi the - today said the approximate site directory structure, which said how to get the site root directory, a lot of classmates are not very understanding, the following step by step: Wu - About $My Site Directory to path: D:\www\message2011, test file path: D:\www\message2011\include\config.inc.PHP - - - ACreate a new file in the root directory, or a separate level of directory creation, as the above example, I would like to Config.inc.gets the root directory of the site in PHP. + the - $1, get the path to the current file: __file__ with Magic constants. The path here contains the file name and the results are as follows: the the the theD:\www\message2011\include\config.inc.PHP - in the the2. Remove the file name from the path above, and get the path pure:dirname(__file__), the results are as follows: About the the theD:\www\message2011\include + - the Bayi3. Return a directory to the root directory:Realpath(dirname(__file__).‘ /.. /‘), the results are as follows: the the - -D:\www\message2011 the the the the4, turn the left "\" into the right "/":Str_replace( ‘\\‘ , ‘/‘ ,Realpath(dirname(__file__).‘ /.. /‘) , the results are as follows: - the the thed:/www/message201194 the the theTo this is what we want the site root directory, can be set according to the requirements of the third step to return the directory layer, such as: Return to the 2 layer is/. /.. /, then you will get D:\www\, and so on. 98 About - 101 Method Two:102 103 104 theAt Global.Inc. defines the root directory106 107 108 109 Define("App_root",dirname(__file__)); the 111 the 113 This constant can be referenced in any PHP file the the the 117 require_once(App_root. " /inc/head.php ");118 119 - 121 122 123 Method Three:124 the 126 127<?PHP - 129 the 131 $PHP _self=$_server[' Php_self ']?$_server[' Php_self ']:$_server[' Script_name ']; the 133 134 135 $url= ' http://'.$_server[' Http_host '].substr($PHP _self, 0,Strrpos($PHP _self, '/') +1);136 137 138 139 Echo $url; $ 141 142 143 method Four:144 145 146 147 $basepath=$_server[' Php_self '];148 149 Max 151 $basepath=substr($basepath, 0,Strpos($basepath, "folder name")); the 153 154 155 Echo $basepath;156 157 158 159For example: You save the file as a.php and the path is:/wjj/wjj1/wjj2/a.PHP the 161 162 163 The above example is written as:164 165 166 167 $basepath=$_server[' Php_self '];168 169 the 171 $basepath=substr($basepath, 0,Strpos($basepath, "Wjj1"));172 173 174 175 Echo $basepath;176 177 178 179 Method Five: the 181 182 183 $basepath=$_server[' Php_self '];184 185 186 187 Preg_match("/(\/)? ([^\/]+)/",$basepath,$WJM);188 189 the 191 Echo $WJM[0];192 193 194 195Note that the second method I am not quite sure, such as the file path is:/wjj/wjj1/wjj2/a.php, it only returns after/WJJ/Add yourself! 196 197 198 199?>
PHP Get site root directory