How to get the absolute path of the current file using DirName (__file__) in PHP

Source: Internet
Author: User
DirName (file) takes the absolute path to the current file, which means that the lookup speed is the fastest compared to the relative path.

PHP constant DirName (file)
File: called the PHP Magic Constant, returns the full path and file name of the currently executing PHP script, containing an absolute path

1) the dirname (FILE) function returns the path where the script is located.

For example, the file b.php contains the following content:

<?php $basedir = dirname (FILE); echo $basedir//will print out an absolute path to this file on the page!?>

I did the test to get the result: e:/website/othertest/cms
This equates to the use of server.mappth in ASP
If b.php is referenced by a a.php file require or include in another directory. The contents of the variable $basedir are also the path to the folder where the b.php resides. Instead of becoming the directory where the a.php file resides.

2) dirname (file) typically returns the current directory of the file to a directory structure in the system root directory.

The current file name is not returned. DirName (FILE) may also return one. (current directory) [The reason is that the b.php file is in the default Web directory of the http.conf or PHP configuration development environment

Several "Magic constants" for PHP

Name Description
The current line number in the lines file.
The full path and file name of the files file. If used in the include file, the include filename is returned. Since PHP 4.0.2, FILE always contains an absolute path, and the previous version sometimes contains a relative path.
Function name (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
The class name (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters.
Method Name (PHP 5.0.0 new). Returns the name of the method when it is defined (case sensitive, such as the current file is placed under (d:\www\), and the file name is test.php.
The code for the test is as follows:

<?php Echo FILE; Get the absolute address of the current file, result: D:\www\test.php echo dirname (file); Get the absolute directory where the current file is located, Result: D:\www\ echo dirname (dirname (file)); Gets the previous level directory name of the current file, results: D:\? >

uses a method hint, and
dirname (file) takes the absolute path to the current file, which means that the lookup speed is the fastest compared to a relative path.
If you can repeat the directory up one level at a time:
For example: $d = DirName (dirname (FILE)); The
is actually a directory to dirname () to do parameters. Because DirName () returns the last directory without \ \ or/
, you can assume that dirname () treats the lowest-level directory as a file name when it is reused. Returns the parent directory of the
current directory as usual. This way, you get the top level of the directory. The

contains the file
include (dirname (file) that gets the top level of the directory. /.. /filename.php '); The path to the

file is the one where the current code is located

dirname (dirname (file)), which is the file's upper directory name

dirname (file);

Related Article

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.