PHP obtains directories and methods through magic variables, Super global variables, and related functions:
The code is as follows: |
Copy code |
<? Php /** * PHP path or directory retrieval implementation * @ Link http://www.111cn.net */ // Magic variable to obtain the absolute path of the current file Echo "_ FILE __:=======>". _ FILE __; Echo '<br/> '; // Magic variable to get the Directory of the current script Echo "_ DIR __:=======>". _ DIR __; Echo '<br/> '; // Dirname indicates the Directory of the returned path. dirname (_ FILE _) is equivalent to _ DIR __ Echo "dirname (_ FILE _): =======>". dirname (_ FILE __); Echo '<br/> '; // $ _ SERVER ['php _ SELF '] and $ _ SERVER ['script _ name'] have the same results. They both obtain the file NAME of the current SCRIPT. // The difference only occurs when php runs in cgi mode, but it cannot be found to run php in cgi mode. Echo '$ _ SERVER ["PHP_SELF"]: =======>'. $ _ SERVER ['php _ SELF ']; Echo '<br/> '; Echo '$ _ SERVER ["SCRIPT_NAME"]: =======>'. $ _ SERVER ['script _ name']; Echo '<br/> '; // The absolute path of the script being executed. Remember, you cannot get it when running php in CLI mode. Echo '$ _ SERVER ["SCRIPT_FILENAME"]: =======>'. $ _ SERVER ['script _ filename']; Echo '<br/> '; // The root directory of the file where the script is currently running. Defined in the server configuration file. Echo '$ _ SERVER ["DOCUMENT_ROOT"]: =======>'. $ _ SERVER ['document _ root']; Echo '<br> '; // Getcwd () returns the current working directory Echo "getcwd (): ======>". getcwd (); Echo '<br> '; Echo '<br> '; Echo "php Tutorial (www.111cn.net ";
|
(Note: $ HTTP_SERVER_VARS and $ _ SERVER are different variables. PHP processes them differently .) If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ SERVER and $ HTTP_SERVER_VARS arrays are separated.
$ _ SERVER ['http _ host'] retrieves the HOST of the current request: header content
$ _ SERVER ['php _ SELF '], which is probably the most frequently used one. It returns the name of the currently called page, if it is http: // localhost/test/2005/test. php,/test/2005/test will be returned. php
$ _ SERVER ['script _ name'] returns the path containing the current SCRIPT. This is useful when the page needs to point to itself.
$ _ SERVER ['script _ filename'] it returns the absolute path of the current file.
$ _ SERVER ['request _ URI '] returns the URI required to access this page, including "/"
Of course there are many path functions:
Dirname (), returns the directory section in the path information, which is prefixed "/"
Basename () returns the basic file name in the path. You can also set a suffix to control the output.
Realpath (), returns the path information of the absolutely normalized path