Home > Backend Development > PHP Tutorial > PHP pathinfo obtains the path, name and other information of the file

PHP pathinfo obtains the path, name and other information of the file

WBOY
Release: 2016-07-29 08:46:42
Original
1005 people have browsed it

Suppose there is an image file now, and its server-side path is:
$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo() function
pathinfo() function returns a file containing An array of information. There are four elements in the array, namely dirname, basename, extension, and filename. Code for printing array:

Copy 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 only need to get the corresponding key value based on the key name of the array:

Copy the code The code is as follows:


echo $fileArr['filename'];
//Output result: myphoto
echo $fileArr['extension'];
//Output result: jpg
//. ..


2.dirname() function
dirname() function gives a string containing the full path to a file. The value it returns is the directory name after removing the file name, which can be considered as a reference to pathinfo() Extension of the function:

Copy the code 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 results are all: /www/mywebsite


So it can be understood that the returned value is the upper directory of the path Address name.
3.basename() function
The basename() function gives a string containing the full path to a file. The value it returns is the basic file name. It can also be considered as an extension of the pathinfo() function:

Copy the code The code is as follows:


echo basename($path);
//Output result: myphoto.jpg
//Or
basename("/www/mywebsite/images/");
// Output result: images


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

The above introduces the description of the path, name and other information of the file obtained by PHP pathinfo, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template