本文分享PHP获取文件扩展名的6种方法,下面是PHP代码:
¥filename = 'mypic.gif';
// 1. The explode/end approach
¥ext = end(explode('.', ¥filename));
// 2. The strrchr approach
¥ext = substr(strrchr(¥filename, '.'), 1);
// 3. The strrpos approach
¥ext = substr(¥filename, strrpos(¥filename, '.') + 1);
// 4. The preg_replace approach
¥ext = preg_replace('/^.*\.([^.]+)¥/D', '¥1', ¥filename);
// 5. The never use this approach
// From: http://php.about.com/od/finishedphp1/qt/file_ext_PHP.htm
¥exts = split([/\\.], ¥filename);
¥n = count(¥exts)-1;
¥ext = ¥exts[¥n];
//6. The pathinfo approach
¥ext = pathinfo(¥filename, PATHINFO_EXTENSION);
?>