<? PHP
Function get_extension ($ file)
{
Return substr (strrchr ($ file, '.'), 1 );
}
Function get_extension2 ($ file)
{
Return substr ($ file, strrpos ($ file, '.') + 1 );
}
Function get_extension3 ($ file)
{
Return end (explode ('.', $ file ));
}
Function get_extension4 ($ file)
{
$ Info = pathinfo ($ file );
Return $ info ['extension'];
}
Function get_extension5 ($ file)
{
Return pathinfo ($ file, pathinfo_extension );
}
$ File = "dgf.fdgf.hhh.pdf ";
Echo get_extension5 ($ file );
// Var_dump (pathinfo );
The above methods seem to work, especially the 1 and 2 methods, which have been used until I know that pathinfo has a second parameter. However, the first four methods have various problems. To obtain the file extension correctly, you must be able to handle the following three special cases.
No file extension
The path contains characters such as/home/test. d/test.txt.
The path contains characters. But the file has no extension. For example,/home/test. d/test
Obviously: 1 and 2 cannot handle the third case, and 3 cannot correctly handle the first three cases. 4. If the extension does not exist, a warning is issued. Only 5th methods are the most correct methods. Take a look at the pathinfo method. The official website is as follows:
$ File_path = pathinfo ('/www/htdocs/your_image.jpg ');
Echo "$ file_path ['dirname'] \ n ";
Echo "$ file_path ['basename'] \ n ";
Echo "$ file_path ['extension'] \ n ";
Echo "$ file_path ['filename'] \ n"; // only in PHP 5.2 +
It returns an array that contains up to four elements, but does not always have four elements. For example, if there is no extension, no extension element exists, therefore, warnings are detected in the 4th methods. However, phpinfo also supports the second parameter. You can pass a constant to specify a part of data to be returned:
Pathinfo_dirname-directory
Pathinfo_basename-file name (including extension)
Pathinfo_extension-Extension
Pathinfo_filename-file name (excluding the extension, php> 5.2)
The values of these four constants are 1, 2, 4, and 8. At the beginning, I thought that multiple constants can be specified through the or operation:
Pathinfo ($ file, pathinfo_extension | pathinfo_filename );
Later, we found that this would not work. This would only return the smallest of several or arithmetic constants. That is, the constant whose minimum bit is 1 in the four flag bits.