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:
The code is as follows:
Echo __file__. "
"; Output F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp\wp_smtp_admin.php
echo dirname (__file__). "
"; Output F:\XAMPP\HTDOCS\WORDPRESS\WP-CONTENT\PLUGINS\WP-SMTP
Echo basename (__file__). "
"; Output wp_smtp_admin.php
second, testing some of WordPress's own functions:
The code is as follows:
echo Plugin_basename (__file__). "
"; Output wp-smtp/wp_smtp_admin.php
Echo dirname (Plugin_basename (__file__)). "
"; Output WP-SMTP
echo Plugin_dir_url (__file__). "
"; Output http://localhost/wordpress/wp-content/plugins/wp-smtp/
echo Plugin_dir_path (__file__). "
"; Output f:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp/
Echo Plugins_url (). "
"; Output Http://localhost/wordpress/wp-content/plugins
Echo Plugins_url (", __file__). "
"; Output HTTP://LOCALHOST/WORDPRESS/WP-CONTENT/PLUGINS/WP-SMTP
echo plugins_url ('/img/blq_32_32.jpg ', __file__). "
"; Output http://localhost/wordpress/wp-content/plugins/wp-smtp/img/blq_32_32.jpg
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.