Example of PHP getting the current path and Directory

Source: Internet
Author: User
Tags php tutorial

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.