php中几种获取文件后缀名的方法
墨初 应用软件 3605阅读
php中有多种获取文件后缀名的方法,比如使用substr()函数与pathinfo()函数等,下面就来介绍几种php中获取文件后缀名的方法,大家各自参考一下!
php获取文件后缀名的方法
示例1:
php substr() 函数获取文件后缀名
function get_extension($file){ return substr(strrchr($file, '.'), 1); }
示例2:
function get_extension2($file){ return substr($file, strrpos($file, '.')+1); }
示例3:
php explode() 函数获取文件的后缀名
function get_extension3($file){ return end(explode('.', $file)); }
示例4:
php pathinfo() 函数获取文件的后缀名!
function get_extension4($file){ $info = pathinfo($file); return $info['extension']; }
示例5:
php pathinfo() 函数中的第二个参数的值如果为 PATHINFO_EXTENSION ,可以直接返回文件的后缀名!
function get_extension5($file){ return pathinfo($file, PATHINFO_EXTENSION); }