How does php obtain the file suffix ,. Suffix; $ img_extsubstr ($ img, strrpos ($ img, how php gets the extension name of a file,
The suffix of a video file, such as jpg or gif.
There are two methods
I. Assume $ img is the image file name.
$img=12345.gif;$img_ext = substr($img, strrpos($img, '.'));
2. use the php array to read the file name string to the array first.
$ Ext2 = explode (". ", $ img); $ count = count ($ ext2); $ count2 = $ count-1; $ file_name = $ urlstr. '. '. $ ext2 [$ count2]; // Get the suffix and redefine the new file name $ file_name
In addition, the methods summarized by other partners are attached:
Method 1st: 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 );}
The above methods seem to work, especially the 1 and 2 methods, which have been used 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
- The path contains characters such as/home/test. d/test.txt.
- The path contains characters. but the file has no extension. For example,/home/test. d/test
Obviously: 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 a part of data to be returned:
PATHINFO_DIRNAME-directory PATHINFO_BASENAME-file name (including extension) PATHINFO_EXTENSION-extension PATHINFO_FILENAME-file name (excluding extension, PHP> 5.2)
The values of these four constants are 1, 2, 4, and 8. at the beginning, I thought that multiple constants can be specified through the OR operation:
Pathinfo ($ file, PATHINFO_EXTENSION | PATHINFO_FILENAME );
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.
Http://www.bkjia.com/PHPjc/1133019.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133019.htmlTechArticlephp how to get the file suffix, compared to the file suffix, jpg or gif, there are two methods one, if $ img for the image file name extension img31612345.gif; $ img_ext = substr ($ img, strrpos ($ img ,...