6 Methods for PHP to obtain the file extension

Source: Internet
Author: User
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 );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.