A case analysis of php__file__, dirname and basename difference usages

Source: Internet
Author: User
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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.