This article mainly introduces the use of file, DirName and basename in PHP, and analyzes the specific usage of file, dirname and basename in the case form, and tests and compares the functions in WordPress, which need friends to refer to.
The full path and file name of the file currently running in PHP, if used in the included file, returns the included file name, which is a magic variable (predefined constant), we can get the file directory or filename by Universal dirname and basename.
First, file and dirname (file) and basename (file) use:
Using dirname (file) is usually a very efficient way to configure files, but because the path to file is the full path to the current code (not the URL), the definition profile is typically placed in the root directory to define the root address of the site. However, the following method can solve the problem of configuration file storage, 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)); Gets the file on the top of the directory name
DirName (file): The name of the directory in which the files are located
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:
Echo FILE. "<br/>"; Output F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp\wp_smtp_admin.php echo dirname (FILE). "<br/>"; Output F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp echo basename (FILE). "<br/>"; Output wp_smtp_admin.php
Second, testing some of WordPress's own functions:
echo Plugin_basename (FILE). "<br/>"; Output wp-smtp/wp_smtp_admin.php echo dirname (Plugin_basename (FILE)). "<br/>"; Output wp-smtp echo plugin_dir_url (FILE). "<br/>"; Output Http://localhost/wordpress/wp-content/plugins/wp-smtp/echo Plugin_dir_path (FILE). "<br/>"; Output F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp/echo Plugins_url (). "<br/>"; Output http://localhost/wordpress/wp-content/plugins echo plugins_url (', FILE). "<br/>"; Output http://localhost/wordpress/wp-content/plugins/wp-smtp echo plugins_url ('/img/blq_32_32.jpg ', FILE) . "<br/>"; 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.