Home > Article > Backend Development > PHP pathinfo obtains the path, name and other information of the file
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 )
Copy the code The code is as follows:
echo $fileArr['filename'];
//Output result: myphoto
echo $fileArr['extension'];
//Output result: jpg
//. ..
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
Copy the code The code is as follows:
echo basename($path);
//Output result: myphoto.jpg
//Or
basename("/www/mywebsite/images/");
// Output result: images
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.