Example of getting the current path and directory with PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:35:56
Original
954 people have browsed it

 

The code is as follows


/**
* PHP obtain path or directory implementation
* @link http://www.45it.com
​*/

//Magic variable, get the absolute path of the current file
echo "__FILE__: ========> ".__FILE__;
echo '
';

//Magic variable, get the directory of the current script
echo "__DIR__: ========> ".__DIR__;
echo '
';

//dirname returns the directory part of the path, dirname(__FILE__) is equivalent to __DIR__
echo "dirname(__FILE__): ========> ".dirname(__FILE__);
echo '
';

//The results of $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] are generally the same. They both obtain the file name of the current script
//There is only a difference when php is run in cgi mode, but now it is almost impossible to find php running in cgi mode
echo '$_SERVER["PHP_SELF"]: ========> '.$_SERVER['PHP_SELF'];
echo '
';

echo '$_SERVER["SCRIPT_NAME"]: ========> '.$_SERVER['SCRIPT_NAME'];
echo '
';

//The absolute path of the currently executing script.Remember, you can’t get it by running php in CLI mode
echo '$_SERVER["SCRIPT_FILENAME"]: ========> '.$_SERVER['SCRIPT_FILENAME'];
echo '
';

//The document root directory where the currently running script is located. Defined in the server configuration file.
echo '$_SERVER["DOCUMENT_ROOT"]: ========> '.$_SERVER['DOCUMENT_ROOT'];
echo '
';

//getcwd() returns the current working directory
echo "getcwd(): ========> ".getcwd();
echo '
';

echo '
';
echo "php tutorial (www.45it.com) finishing";

(Note: $HTTP_SERVER_VARS and $_SERVER are different variables, and PHP handles them differently.) These variables are also available in all scripts if the register_globals directive is set; that is, the $_SERVER and $HTTP_SERVER_VARS arrays are separated .

 $_SERVER['HTTP_HOST'] Get the Host: header content of the current request

 $_SERVER['PHP_SELF'] This may be the one we use most often. It returns the file name of the currently called page. If it is http://localhost/test/2005/test.php, then it will Return to /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'] will return the absolute path information 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 part of the path information, preceded by "/"

Basename() returns the basic file name part of the path. Of course, you can also set the suffix to control the output.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/741452.htmlTechArticleThe code is as follows?php /*** PHP obtains path or directory implementation * @link http://www.45it.com*/ //Magic variable, get the absolute path of the current file echo __FILE__: ======== .__FILE__; echo 'br...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template