PHP PathInfo () Get the file path, name and other information description _php tips

Source: Internet
Author: User
Suppose you now have a picture file, and its server-side path is:

$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo () function
The PathInfo () function returns an array of file information containing four elements, namely, dirname, basename, extension, filename. To print the array code:
Copy Code code as follows:

$FILEARR = PathInfo ($path);
Print_r ($FILEARR);
Output result: Array ([dirname] =>/www/mywebsite/images [basename] => myphoto.jpg [extension] => jpg [filename] => m Yphoto)

So we just have to get the corresponding key value based on the key name of the array:

Copy Code code as follows:

echo $fileArr [' filename '];
Output Result: Myphoto
echo $fileArr [' extension '];
Output results: JPG
//...

2.dirname () function
The dirname () function gives a string containing a full path to a file, and the value returned removes the directory name after the file name, which can be considered an extension of the PathInfo () function:
Copy Code code as follows:

echo dirname ($path);
Output Result:/www/mywebsite/images
Or
Echo dirname ("/www/mywebsite/images/");
Echo dirname ("/www/mywebsite/images");
The results of the output are:/www/mywebsite

So it can be understood that the returned value is the directory address name of the upper level of the path.
3.basename () function
The basename () function gives a string containing a full path to a file, and the value he returns is a basic filename, which can also be considered an extension of the PathInfo () function:
Copy Code code as follows:

Echo basename ($path);
Output Result: myphoto.jpg
Or
BaseName ("/www/mywebsite/images/");
Output Result: Images

So you can understand the name of the current directory where the returned value is 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.