?
mono', 'courier new', courier, monospace; color: #c00000; font-size: 14px;">PHP:6种方法获取文件的扩展名
?
1、字符串查找和截取的方法
?
1class="php variable" style="background-image: none !important; text-align: left !important; line-height: 1.1em !important; font-style: normal !important; margin: 0px; width: auto !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; float: none !important; color: #aa7700 !important; font-size: 1em !important; vertical-align: baseline !important; overflow: visible !important; font-weight: normal !important; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border: 0px; padding: 0px !important;">$extension
=
functions" style="background-image: none !important; text-align: left !important; line-height: 1.1em !important; font-style: normal !important; margin: 0px; width: auto !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; float: none !important; color: #ff1493 !important; font-size: 1em !important; vertical-align: baseline !important; overflow: visible !important; font-weight: normal !important; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border: 0px; padding: 0px !important;">substr
(
strrchr
(
$file
,?
'.'
), 1);
2、字符串查找和截取的方法二
?
1$extension
=
substr
(
$file
,?
strrpos
(
$file
,?
'.'
)+1);
3、数组分割的方法
?
1$extension
=
end
(
explode
(
'.'
,?
$file
));
4、使用pathinfo直接解析的方法
?
1 2$info
?=?
pathinfo
(
$file
);
$extension
=
$info
[
'extension'
];
5、使用pathinfo的第二个参数
?
1$extension
=
pathinfo
(
$file
, PATHINFO_EXTENSION);
6、使用finfo_file函数
?
1 2 3 4$finfo
?= finfo_open(FILEINFO_MIME_TYPE);
$extension
?= finfo_file(
$finfo
,?
$file
) ;
echo
?$extension
;
finfo_close(
$finfo
);