PHP pathinfo () obtains the file path, name, and other information descriptions.

Source: Internet
Author: User

Suppose there is an image file, and its server path is:

$ Path = "/www/mywebsite/images/myphoto.jpg ";
1. pathinfo () function
The pathinfo () function returns an array containing file information. The array contains four elements: dirname, basename, extension, and filename. Print the array code:
Copy codeThe Code is as follows:
$ FileArr = pathinfo ($ path );
Print_r ($ fileArr );
// Output result: Array ([dirname] =>/www/mywebsite/images [basename] => myphoto.jpg [extension] => jpg [filename] => myphoto)

In this way, we can obtain the corresponding key value based on the array key name:

Copy codeThe Code is as follows:
Echo $ fileArr ['filename'];
// Output result: myphoto
Echo $ fileArr ['extension'];
// Output result: jpg
//...

2. dirname () function
The dirname () function provides a string containing a full path to a file. The returned value removes the directory name after the file name. It can be considered an extension of the pathinfo () function:
Copy codeThe Code is as follows:
Echo dirname ($ path );
// Output result:/www/mywebsite/images
// Or
Echo dirname ("/www/mywebsite/images /");
Echo dirname ("/www/mywebsite/images ");
// The output result is/www/mywebsite.

Therefore, it can be understood that the returned value is the directory address name at the top layer of the path.
3. basename () function
The basename () function provides a string containing a full path pointing to a file. The returned value is the basic file name. It can also be considered an extension of the pathinfo () function:
Copy codeThe Code is as follows:
Echo basename ($ path );
// Output result: myphoto.jpg
// Or
Basename ("/www/mywebsite/images /");
// Output result: images

Therefore, it can be understood that the returned value is the name of the current directory of the path.

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.