$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:
Mixed pathinfo (string $path [, int $options = Pathinfo_dirname | Pathinfo_basename | pathinfo_extension | Pathinfo_filename])
pathinfo ()Returns an associative array containing the information with path. Whether to return an associative array or a string depends on the options.
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