PHP dirname () function

Source: Internet
Author: User
Definition and usage
The DirName () function returns the directory portion of the path.

Grammar
DirName (path) parameter description
Path is required. 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

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


Output:

C:/testweb
/testweb

A useful constant is defined in PHP, which is

__file__

The default constant is the full path (path + filename) of the current PHP program.

Even if the file is referenced by another file (include or require), __file__ is always the full path to the file it resides in, not the full path to the file that references it.

Take a look at the following example:

/home/data/demo/test/a.php

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

home/data/demo/b.php

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


To put it simply:
__FILE__ returns the current path + file name
DirName (__file__) returns the path portion of the current file path
DirName (DirName (__file__)); Gets the file on the top of the directory name (does not include the last "/" number)

For example, if the current file is/home/data/demo/test.php, the
__FILE__ get is the full path is/home/data/demo/test.php, and
DirName (__file__) Gets the path part that is the/home/data/demo (no "/" number behind)

  • 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.