PHP get the current path and directory example

Source: Internet
Author: User
Tags php tutorial root directory


code as follows  

 * php Fetch path or directory implementation
 * @l Ink
//Magic variable, get the absolute path of the current file
echo "__file__:  ========> ". __FILE__;&NBSP
Echo ' <br/> ';
//Magic variable, get directory of current script
echo "__dir__:  ========> ". __dir__;
Echo ' <br/> ';
//dirname The directory portion of the path, DirName (__file__) is equivalent to __dir__
Echo dirname (__file__):    ". DirName (__file__);
Echo ' <br/> '; The results of
//$_server[' php_self ' and $_server[' script_name ' are generally the same, and they all get the file name of the current script
//Only when PHP is running in CGI, But now it's almost impossible to find a CGI run PHP
Echo ' $_server[' php_self ']:  ========>  '. $_server[' php_self '];
Echo ' <br/> ';
Echo ' $_server[' script_name ']:  ========>  '. $_server[' Script_NAME '];
Echo ' <br/> ';
//The absolute path of the current execution script. Remember, running PHP in CLI mode is not getting the
Echo ' $_server[' script_filename ']:  ========>  '. $_server[' Script_filename '] ;
Echo ' <br/> ';
//The document root directory where the currently running script resides. 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 ( organize ";

(Note: $HTTP _server_vars and $_server are different variables, PHP handles them in different ways.) If the register_globals directive is set, these variables are also available in all scripts, that is, the $_server and $HTTP _server_vars arrays are separated.

$_server[' Http_host ' gets the current requested HOST: header content

$_server[' php_self '] This is probably the most frequently used, it returns the file name of the page that is currently being invoked, and if it is http://localhost/test/2005/test.php, it will return/test/2005/ test.php

$_server[' Script_name '] it will return the path containing the current script. This is useful when the page needs to point to itself

$_server[' Script_filename '] it will return the absolute path information where the current file is located

$_server[' Request_uri ' returns the URI required to access this page, including the "/"

There are, of course, many path functions:

DirName (), returns the section of the directory in the path information, preceded by a "/"

BaseName () returns the basic file name portion of the path, and can, of course, set the suffix to control the output.

Realpath (), returns the absolutely normalized path of the path information

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: 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.