N methods for getting file extensions in PHP

Source: Internet
Author: User

In PHP, N methods for obtaining file extensions are collected from the Internet, basically in the following ways: 1st Methods: function get_extension ($ file) {substr (strrchr ($ file, '. '), 1);} 2nd Methods: function get_extension ($ file) {return substr ($ file, strrpos ($ file ,'. ') + 1);} 3rd Methods: function get_extension ($ file) {return end (explode ('. ', $ file);} 4th Methods: function get_extension ($ file) {$ info = pathinfo ($ file); return $ info ['extension'];} method 5th: function get_extension ($ file) {return pathinfo ($ file, PATHINFO_EXTENSION);} a rough look at the above methods. It seems to be okay, especially the 1 and 2 methods. They are still in use 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 path contains characters. For example, the path/home/test. d/test.txt contains characters. But the file does not have an 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. $ 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 containing up to four elements, but it 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, 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.

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.