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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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.