DirName (__file__) detailed

Source: Internet
Author: User
__FILE__ indicates that the absolute path to the current file includes the filename, dirname (__file__) represents the absolute path of the current file, and basename (__file__) represents the file name of the current file, DirName (__file__). " /f/". BaseName (__file__) is the directory under the current file under the F directory, the file name is called dirname (__file__) files, require represents the file contained in this file.

Here is an explanation:

1) the dirname (__file___) function returns the path where the script is located.

For example, the file b.php contains the following content:

<?php$basedir = DirName (__file__);? >

If b.php is referenced by a a.php file require or include in another directory.
The contents of the variable $basedir are also the path to the folder where the b.php resides.
Instead of becoming the directory where the a.php file resides.

2) dirname (__file__) typically returns a directory structure of the current directory of the file to the system root directory.

The current file name is not returned.
DirName (__file__) may also return one. (current directory)
[The reason is that the b.php file is under the default Web directory of the http.conf or PHP Configuration development environment.]
For example web_root: "c:/root/www/".
The b.php file path is: "c:/root/www/b.php".

3) Using the method tips,

If you repeat, you can raise the directory up one level:

Like what:$d = dirname(dirname(__FILE__));

The fact is that a directory to dirname () to do the parameters. Because DirName () returns the final directory without \ \ or
So when you reuse it, you can think of dirname () handling the lowest-level directory as a file name. Return as usual
The parent directory of the current directory. That way, you get the top level of the directory.

4) file containing the previous level of the directory

include(dirname(__FILE__).''/../filename

The difference between PHP dirname (__file__) and DirName (DirName (__file__))

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.

script of the small note: it is actually used PHP's DirName () function and __file__

Definition and usage
The DirName () function returns the directory portion of the path.

Grammar

DirName (PATH)

Parameters Description
Path Necessary. Specifies the path to check.

Description
The path parameter is a string that contains a full path to a file. This function returns the name of the directory after the file name has been removed.

Example

<?phpecho dirname ("c:/testweb/home.php"); Echo dirname ("/testweb/home.php"); >

Output:

C:/testweb
/testweb

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.