PHP gets the file name and extension

Source: Internet
Author: User

1.basename ()-Returns the file name of the path
Take a look at the following PHP code:

1<?PHP2 3 $path= "/usr/www/html/index.php";4 5 Echo basename($path)." <br> ";6 7 //If you select suffix, the extension is ignored8 9 Echo basename($path, ". PHP");Ten  One?>

Operation Result:
index.php
Index
2.dirname ()-Returns the file path of the current script!
PHP Code:

    1. <? PHP
    2. --file__ return file Full path
    3. $dir = dirname(__file__);
    4. echo $dir;
    5. ?>

Operation Result:
F:\web\zend\exercise
3.pathinfo () returns an associative array containing information about path.
Includes the following array cells: path name dirname, file name basename, and extension name extension.
Take a look at the following simple code demo:

    1. <? PHP
    2. $path = "/usr/www/html/index.php";
    3. $pathinfo = pathinfo($path);
    4. echo "Catalog name: $pathinfo [dirname]<br>";
    5. echo "file name: $pathinfo [basename]<br>";
    6. echo "extension: $pathinfo [extension]";
    7. ?>


Operation Result:
Catalog Name:/usr/www/html
File name: index.php
Extension: PHP
4.realpath--Returns the normalized absolute path name
The PHP code is as follows:

    1. <? PHP
    2. $path = "./exercise/php.txt";
    3. $realpath = realpath($path);
    4. echo $realpath;
    5. ?>


Finally note a tip: Different paths of File path operators may, different, Windows can use "/" and "\",
Linux can only use "/", so the development of the time, the recommendations are used "/", such as my file path above the wording!

PHP gets the file name and extension

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.