Home > Backend Development > PHP Tutorial > PHP pathinfo() Gets the path, name and other information of the file_PHP Tutorial

PHP pathinfo() Gets the path, name and other information of the file_PHP Tutorial

WBOY
Release: 2016-07-21 15:24:45
Original
1331 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
The pathinfo() function returns an array containing file information. There are four elements in the array, namely dirname, basename, extension, and filename. Code to print 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 can get the corresponding key value just based on the key name of the array:

Copy 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 character containing the full path to a file string, the value it returns is the directory name after removing the file name, which can be considered as an extension of the pathinfo() function:
Copy 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: /www/mywebsite

So it can be understood that the returned value is the path The address name of the upper-level directory.
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, which can also be considered It is an extension of the pathinfo() function:
Copy 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324253.htmlTechArticleSuppose there is an image file and its server-side path is: $path = "/www/mywebsite/images /myphoto.jpg"; 1.pathinfo() function The pathinfo() function returns a file containing the file information...
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