Php dirname (_ FILE _) and _ FILE _ Constants

Source: Internet
Author: User

In php, dirname () and _ FILE _ are our constants. This article will give you a detailed introduction to some issues in the use of dirname () and _ FILE _ constants, if you are interested, go to the reference page.

Dirname () function

String dirname (string $ path)

A string containing a full path pointing to a file is provided. This function returns the directory name after removing the file name.

In Windows, both slash (/) and backslash () can be used as Directory separators. It is a slash (/) in other environments (/).


Example #1 dirname ()

Example

The Code is as follows: Copy code

Echo dirname ("c:/testweb/home. php ");
Echo dirname ("/testweb/home. php ");
?>

Output:

C:/testweb
/Testweb

_ FILE __

The complete file path and file name. If it is used in a file to be included, the file name to be included is returned.

Dirname (_ file __)
_ FILE _: a PHP magic constant. It returns the complete path and FILE name of the currently executed PHP script, including an absolute path.
1) The dirname (_ FILE _) function returns the path of the script. Update network
For example, file B. php contains the following content:

The Code is as follows: Copy code

$ Basedir = dirname (_ FILE __);
Echo $ basedir
// An absolute path of the file will be printed on the page!
?>

The result of my test is: E: websiteothertestcms.

Dirname (_ FILE _) usually returns a directory structure from the current directory of the FILE to the root directory of the system.

The current file name is not returned.

Dirname (_ FILE _) may also return a. (current directory)
[The reason is that the B. php file is in the default WEB directory where http. conf or PHP is used to configure the development environment.
For example, WEB_ROOT is: "C:/root/www/".]

B. The php file path is "C:/root/www/B. php". The usage prompt is displayed. If you repeat it once, you can upgrade the directory to a higher level:
For example: $ d = dirname (_ FILE __));
In fact, a directory is used as a parameter to dirname (), because the last directory returned by dirname () does not contain \ or/
Therefore, we can think that dirname () treats the bottom directory as a file name.
The parent directory of the current directory. in this way, the directory at the upper level is obtained. include (dirname (_ FILE __). '/.. /filename. php ');

2. How to combine PHP files with html files

The Code is as follows: Copy code
If ($ reguser! = ""){
Echo "Submit Photo
";
Echo "Log Out ";
} Else {
Echo "Login ";
}
?>

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.