Home >Backend Development >PHP Problem >What are the functions in php to get the file suffix name?
php file upload and multi-file upload are important and must be mastered. There are many methods for file upload. Here we mainly introduce several of them for your reference. For everyone’s reference.
1. String search and segmentation method
1.$file = 'x.y.z.png'; echo substr(strrchr($file, '.'), 1);
Analysis: strrchr($file, '.')
strrchr() function searches for strings The position of the last occurrence in another string and returns all characters from that position to the end of the string
2.$file = 'x.y.z.png'; echo substr($file, strrpos($file, '.')+1);
Parsing: strrpos($file, '.')
Find " ." At the last position in the string, return the position substr() intercepts from this position
2. Array splitting method
3.$file = 'x.y.z.png'; $arr=explode('.', $file); echo $arr[count($arr)-1]; 4.$file = 'x.y.z.png'; $arr=explode('.', $file); echo end($arr); 5.$file = 'x.y.z.png'; echo strrev(explode('.', strrev($file))[0]);
Analysis :end() returns the last element of the array, and the strrev(string) function reverses the string.
3. Path function method
6.$file = 'x.y.z.png'; echo pathinfo($file)['extension']; //参数如下 [dirname] [basename] [extension] 7.$file = 'x.y.z.png'; echo pathinfo($file, PATHINFO_EXTENSION);
Analysis: The pathinfo() function returns the file path information in the form of an array.
The above introduces a total of 7 methods to obtain file extension names, for your reference only. If you want to know more related issues, please visit the PHP Chinese website: PHP Video Tutorial
The above is the detailed content of What are the functions in php to get the file suffix name?. For more information, please follow other related articles on the PHP Chinese website!