Home > Backend Development > PHP Tutorial > How to use PHP __FILE__ to get the absolute path of a file_PHP tutorial

How to use PHP __FILE__ to get the absolute path of a file_PHP tutorial

WBOY
Release: 2016-07-20 11:02:48
Original
1074 people have browsed it

PHP constant dirname(__file__)

__FILE__: Known as PHP magic constant, returns the full path and file name of the currently executing PHP script, including an absolute path

1) The dirname(__FILE__) function returns the path where the script is located. ​​Update network
For example, the file b.php tutorial contains the following content:
                                                                                                                echo $basedir //The absolute path to where this file is located will be printed out on the page!
?>
The test I did got the result: E:websiteothertestcms
This is equivalent to the usage of server.mappth in asp tutorial

If b.php is referenced by a.php file require or include in other directories. The content of the variable $basedir is still the path to the folder where b.php is located. Rather than becoming the directory where the a.php file is located.

2) dirname(__FILE__) generally returns a directory structure from the current directory where the file is located to the system root directory.

The current file name will not be returned. dirname(__FILE__) may also return a . (current directory) [The reason is that the b.php file is in http.conf or the default WEB directory of the PHP configuration development environment

echo __FILE__ ; // Get the absolute address of the current file, result: D:wwwtest.php

echo dirname(__FILE__); // Get the absolute directory where the current file is located, result: D:www echo dirname(dirname(__FILE__)); //Get the upper directory name of the current file, result: D:
?>
===========PHP Get absolute file path========

===========chdir() function===========

Definition and Usage

The chdir() function changes the current directory to the specified directory.

If successful, the function returns true, otherwise it returns false.

Grammar

chdir(directory) parameter description

directory required. Specifies the new current directory.
Example

//Get the current directory

echo getcwd(); echo "
";

//Change to images directory

chdir("images");

echo "
";
echo getcwd();
?> Output:

C:testwebmain

C:testwebmainimages


===========chdir() function ===========


The chdir() function changes the current directory to the specified directory.


If successful, the function returns true, otherwise it returns false.

Grammar

chdir(directory) parameter description

directory required. Specifies the new current directory.
Example

//Get the current directory

echo getcwd(); echo "
";

//Change to images directory

chdir("images");

echo "
";
echo getcwd();
?>


http://www.bkjia.com/PHPjc/445345.html

truehttp: //www.bkjia.com/PHPjc/445345.htmlTechArticlePHP constant dirname(__file__) __FILE__: called PHP magic constant, returns the full path and sum of the currently executed PHP script File name, including an absolute path 1) The dirname(__FILE__) function returns...
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