Example analysis of __file__, dirname and basename usages in PHP _php
Source: Internet
Author: User
KeywordsPhp__file__dirnamebasename
The example in this paper describes the usage of __file__, dirname and basename in PHP. Share to everyone for your reference. Here's how:
__file__ the full path and file name of the currently running file in PHP, and if used in the included file, returns the included file name, which is a magic variable (predefined constant), and we can get the file directory or filename by Universal dirname and basename.
One, __file__ and DirName (__file__) and basename (__file__) use:
Using DirName (__file__) is usually a very efficient way to configure file paths, but because __file__ 's path is the full path to the file (not the URL) where the current code resides, the definition profile is typically placed at the root of the Web site where it is defined. However, the following method can solve the problem of configuration file storage, the code is as follows:
The code is as follows:
DirName (DirName (__file__));
Assuming __file__ is/home/web/config/config.php, the above method output is/home/web
DirName (DirName (__file__)); Get the file on the top of the directory name
DirName (__file__); Gets the directory name of the file.
Suppose the current directory and file structure are as follows, the variables we want to test are in the wp_smtp_admin.php file:
wp-content\plugins\wp-smtp\wp-smtp.php
wp-content\plugins\wp-smtp\wp_smtp_admin.php
Wp-content\plugins\wp-smtp\img\blq_32_32.jpg
The test results are as follows, with the following code:
Okay, now let's take a look at this. DirName and basename related instructions
The DirName () function returns the directory portion of the path, and the basename () function returns the part of the file name in the path from which we can see the result.
I hope this article is helpful to everyone's PHP programming.
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.