PHPpathinfo () obtains the file path, name, and other information descriptions.
Source: Internet
Author: User
In PHP, it is very easy to obtain the path, name, or extension of a file through a function. You can use dirname (), basename (), pathinfo (), and other methods to obtain relevant information. 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:
The 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:
The 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:
The 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:
The 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.
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.