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 "; } ?> |