PHP一些小函数,特别是关于一些文件系统的小函数,老是不能正确处理中文,在使用的时候特别要注意这方面的问题,要么尽可能使用中文文件名,要么就本身写一些小函数替代她们。php
今天又发现了一个,pathinfo在处理中文文件名时就出现了问题(在windows系统内下正常,到到Linux下就异常了windows
输出结果为:ide
能够看到,当连字符“-”出如今英文文件名当中时是没有问题的,但若是文件名是非英文字符,pathinfo函数返回的结果就有可能出现错误.函数
因此只能本身写个函数处理这个问题了spa
function
my_path_info(
$filepath
) {
$path_parts
=
array
();
$path_parts
[
'dirname'
] =
substr
(
$filepath
, 0,
strrpos
(
$filepath
,
'/'
));
$path_parts
[
'basename'
] =
substr
(
$filepath
,
strrpos
(
$filepath
,
'/'
));
$path_parts
[
'extension'
] =
substr
(
strrchr
(
$filepath
,
'.'
), 1);
$path_parts
[
'filename'
] =
substr
(
$path_parts
[
'basename'
], 0,
strrpos
(
$path_parts
[
'basename'
],
'.'
));
return
$path_parts
;
}
上面状况形于路径”/usr/home/www/新建文件.rar”没问题,但当碰到无路径的文件”新建文件.rar”就会出现第一个中文被分割的现象,若是将上面的函数修改成:code
function
my_path_info(
$filepath
) {
$path_parts
=
array
();
$path_parts
[
'dirname'
] = rtrim(
substr
(
$filepath
, 0,
strrpos
(
$filepath
,
'/'
)),
"/"
).
"/"
;
$path_parts
[
'basename'
] = ltrim(
substr
(
$filepath
,
strrpos
(
$filepath
,
'/'
)),
"/"
);
$path_parts
[
'extension'
] =
substr
(
strrchr
(
$filepath
,
'.'
), 1);
$path_parts
[
'filename'
] = ltrim(
substr
(
$path_parts
[
'basename'
], 0,
strrpos
(
$path_parts
[
'basename'
],
'.'
)),
"/"
);
return
$path_parts
;
}