Home >Backend Development >PHP Tutorial >Example analysis of the differences between php__FILE__, dirname and basename

Example analysis of the differences between php__FILE__, dirname and basename

伊谢尔伦
伊谢尔伦Original
2017-06-29 10:59:201252browse

This article mainly introduces the usage of FILE, dirname and basename in PHP. It analyzes the specific usage of FILE, dirname and basename in detail in the form of examples, and tests and compares them. The function that comes with WordPress, friends who need it can refer to

The full path and file name of the FILE currently running file in php, if used in the included file, returns the included file name, which is a magic variable (predefined constant). We can use dirname and basename to get the file directory or file name.

1. Use of FILE, dirname (FILE) and basename (FILE):

Usually using dirname (FILE) when configuring the file path is a very effective method. However, because the path of FILE is the full path of the file where the current code is located (not the file where the url is located), the defined configuration file is usually placed in the root directory to define the root address of the website. However, the following method can solve the problem of storing the configuration file. Code As follows:

dirname(dirname(FILE));

Assume that FILE is /home/web/config/config.php, the output of the above method is /home/web

dirname(dirname(FILE)); the result is the file The directory name of the upper layer

dirname(FILE); what you get is the directory name of the layer where the file is located

Assume that the current directory and file structure is 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, the code is as follows:

echo FILE . "<br />"; //输出 F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp\wp_smtp_admin.php 
echo dirname(FILE) . "<br />"; //输出 F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp 
echo basename(FILE) . "<br />"; //输出 wp_smtp_admin.php


2. Test some functions that come with wordpress:

echo plugin_basename(FILE) . "<br />"; //输出wp-smtp/wp_smtp_admin.php 
echo dirname(plugin_basename(FILE)) . "<br />"; //输出 wp-smtp 
echo plugin_dir_url(FILE) . "<br />"; //输出 http://localhost/wordpress/wp-content/plugins/wp-smtp/ 
echo plugin_dir_path(FILE) . "<br />"; //输出 F:\xampp\htdocs\wordpress\wp-content\plugins\wp-smtp/ 
echo plugins_url() . "<br />"; //输出 http://localhost/wordpress/wp-content/plugins 
echo plugins_url(&#39;&#39;,FILE) . "<br />"; //输出 http://localhost/wordpress/wp-content/plugins/wp-smtp 
echo plugins_url(&#39;/img/blq_32_32.jpg&#39;,FILE)  . "<br />"; //输出 http://localhost/wordpress/wp-content/plugins/wp-smtp/img/blq_32_32.jpg


Okay, now let’s take a look at the instructions related to dirname and basename

dirname() function returns the directory part of the path, and basename() function returns the file in the path Name part, from here it is not difficult for us to see the above results.

The above is the detailed content of Example analysis of the differences between php__FILE__, dirname and basename. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn