Php can be used to obtain File Extensions. For more information, see. Method 1: function get_extension($file){substr(strrchr($file, '.'), 1);} Method 2: function get_extension($file){return substr($file, strrpos($file, '.')+1);} Method 3: function get_extension($file){return end(explode('.', $file));} Method 4: function get_extension($file){$info = pathinfo($file);return $info['extension'];} Method 5: function get_extension($file){return pathinfo($file, PATHINFO_EXTENSION);} The methods above seem to be okay, especially the methods 1 and 2. However, if you understand the second parameter of pathinfo, you will doubt the above method. 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. for example, the path/home/test. d/test.txt contains characters. but the file has no extension. For example,/home/test. d/test is obvious: 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 the data returned to a certain part: PATHINFO_DIRNAME-directory PATHINFO_BASENAME-file name (including the 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 we could specify multiple pathinfo ($ file, PATHINFO_EXTENSION | PATHINFO_FILENAME) through the OR operation ); later, I found that it is not possible. this will only return the smallest of several or arithmetic constants. That is, the constant whose minimum bit is 1 in the four flag bits. |