php 文件扩展名获取方法汇总

发布时间:2019-11-22编辑:脚本学堂
获取文件扩展名的方法,脚本学堂为大家介绍了很多了,今天再为大家介绍五个,够给力吧,哈哈。抓紧时间学习下吧。

php 文件扩展名的获取代码。
 

复制代码 代码示例:

<?php
//取文件的扩展名
//by http://www.jb200.com
$file = "/home/jbxue/file_20130322.txt";

for($i=1; $i < 6; $i++) {
$func = 'get_file_ext_' . $i;
var_dump($func($file));
}

function get_file_ext_1($file) {
return strtolower(trim(substr(strrchr($file, '.'), 1)));
}

function get_file_ext_2($file) {
return strtolower(trim(pathinfo($file, PATHINFO_EXTENSION)));
}

function get_file_ext_3($file) {
return strtolower(trim(substr($file, strrpos($file, '.')+1)));
}

function get_file_ext_4($file) {
return strtolower(trim(array_pop(explode('.', $file))));
}

function get_file_ext_5($file) {
$tok = strtok($file, '.');
while($tok !== false) {
$return = $tok;
$tok = strtok('.');
}
return strtolower(trim($return));
}
?>

附:php 文件扩展名小知识
文件扩展名是操作系统用来标志文件格式的一种机制。
通常来说,一个扩展名是跟在主文件名后面的,由一个分隔符分隔。
在一个像“readme.txt”的文件名中,readme是主文件名,txt为扩展名,表示这个文件被认为是一个纯文本文件。