pathinfo
\ basename
解析带中文路径时不正确
$p1 = pathinfo('atlas/这是文件夹/可爱胖胖是怎么变帅的#超能陆战队.mp4');
$p2 = pathinfo('atlas/dirname/sssss#ddddd.mp4');
print_r($p1);
print_r($p2);
上面的代码直接结果中看到中文的路径解析的时候basename
和filename
是不正确的,而英文路径确实是没有问题的。
basename
也同样存在这个问题
print_r(basename('atlas/这是文件夹/可爱胖胖是怎么变帅的#超能陆战队.mp4'));
echo "\n";
print_r(basename('atlas/dirname/sssss#ddddd.mp4'));
如何解决
打开官方文档可以看到这个警告
pathinfo() is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function.
basename() is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function.
这个两个函数都和locale
有关系,那么在解析中文的时候我们只要通过setlocale
函数设置下就好了。
setlocale(LC_ALL, "zh_CN.UTF-8");
注意
-
因为受字节数的影响,中文路径情况下上面解析错误的情况不一定复现。例如把上面的路径修改为了
atlas/这是文件夹/可爱胖胖是怎么变帅的#超能陆战队.mp4
就不会有问题,所以可能会出现一会儿有问题一会儿没有问题的神奇现象。 -
一定要注意
setlocale
函数的一个Warning(这个警告在中文文档中没有The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS, HHVM or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().