basename fix无法捕捉中文文件名

场景

excel导出统计文件的功能,文件名是还有中文, 排查代码 发现在fileDownload的时候,basename只是
索取到了非中文的部分

分析

官方手册 https://secure.php.net/manual/zh/function.basename.php

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.
不同的编码方式是会对结果造成不同的影响, 所以需要设置本地化或者自己实现

解决

  • 本地化设置
    • setlocale(LC_ALL, ‘zh_CN.GBK’); // windows
    • setlocale(LC_ALL, ‘zh_CN.UTF8’); // linux
  • 自己实现(官方文档的推荐表)
    • end(explode(‘/’,$file_name));
$file_name = '/home/data/www/liusen/bmp-mgmt.dianhua.cn/cache/邦秒配总计详情文档-20180618-20180718.xlsx';
setlocale(LC_ALL, 'zh_CN.GBK'); // windows
echo basename($filename) . PHP_EOL;

猜你喜欢

转载自blog.csdn.net/cominglately/article/details/81099247
今日推荐