Yesterday, I talked to a friend in a PHP chat group (276167802, verification: csl, if you are interested, you can join in and discuss it) and mentioned six methods for PHP to get the file extension, now let's share with you: 1. string search and truncation method $ extension = subst yesterday, a friend in a PHP chat group (276167802, verification: csl, if you are interested, you can join the discussion .)
PHPYou can use the following six methods to obtain the file extension:
1. string search and truncation methods
$ Extension = substr (strrchr ($ file, '.'), 1 );
2. Method 2 for string search and truncation
$ Extension = substr ($ file, strrpos ($ file, '.') + 1 );
3. array partitioning
$ Extension = end (explode ('.', $ file ));
4. direct parsing using pathinfo
$ Info = pathinfo ($ file); $ extension = $ info ['extension'];
5. use the second parameter of pathinfo
$ Extension = pathinfo ($ file, PATHINFO_EXTENSION );
6. use the finfo_file function
$ Finfo = finfo_open (FILEINFO_MIME_TYPE); $ extension = finfo_file ($ finfo, $ file); echo $ extension; finfo_close ($ finfo );