Suppose you now have a picture file, and its server-side path is:
$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo () function
The PathInfo () function returns an array of file information containing four elements, namely, dirname, basename, extension, filename. To print the array code:
Copy Code code as follows:
$FILEARR = PathInfo ($path);
Print_r ($FILEARR);
Output result: Array ([dirname] =>/www/mywebsite/images [basename] => myphoto.jpg [extension] => jpg [filename] => m Yphoto)
So we just have to get the corresponding key value based on the key name of the array:
Copy Code code as follows:
echo $fileArr [' filename '];
Output Result: Myphoto
echo $fileArr [' extension '];
Output results: JPG
//...
2.dirname () function
The dirname () function gives a string containing a full path to a file, and the value returned removes the directory name after the file name, which can be considered an extension of the PathInfo () function:
Copy Code code as follows:
echo dirname ($path);
Output Result:/www/mywebsite/images
Or
Echo dirname ("/www/mywebsite/images/");
Echo dirname ("/www/mywebsite/images");
The results of the output are:/www/mywebsite
So it can be understood that the returned value is the directory address name of the upper level of the path.
3.basename () function
The basename () function gives a string containing a full path to a file, and the value he returns is a basic filename, which can also be considered an extension of the PathInfo () function:
Copy Code code as follows:
Echo basename ($path);
Output Result: myphoto.jpg
Or
BaseName ("/www/mywebsite/images/");
Output Result: Images
So you can understand the name of the current directory where the returned value is the path.