Analysis of __file__, dirname and basename usages in PHP _php techniques

Source: Internet
Author: User
Tags php programming

This article describes the __file__, dirname and basename usages in PHP. Share to everyone for your reference. The specific methods are as follows:

__file__ the full path and filename of the current running file in PHP, if used in the included file, returns the included file name, which is a magical variable (predefined constant), and we can get the file directory or file name by Universal DirName and basename.

__file__ and DirName (__file__) and basename (__file__) use:

It is often useful to use dirname (__file__) When configuring a file path, but since the __file__ path is the full path of the file in which the current code resides, rather than the file where the URL resides, the definition profile is usually placed in the root directory to define the root address of the site. However, the following methods can solve the problem of storage of the configuration file, the code is as follows:

Copy Code code as follows:
DirName (DirName (__file__));

Assuming __file__ is/home/web/config/config.php, the above method output is/home/web

DirName (DirName (__file__)); Got a directory name on the file.

DirName (__file__): The name of the directory in which the file resides

Assuming the current directory and file structure are as follows, we are testing the variables 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, and the code is as follows:

Copy Code code as follows:
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, the test WordPress with some functions:
Copy Code code as follows:
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 instructions

The DirName () function returns the directory portion of the path, and the basename () function returns the filename part of the path from which we can see the result.

I hope this article will help you with your PHP programming.

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.