http://bugs.php.net/bug.php?id=37738
Problem Description:
------------
Simply put, basename() does ot work with Japanese filepaths. If the filename is Japanese only the extension part of the filename is returned. So a filename "/folder/�t�@�C����.txt" resolves to just ".txt". I discovered the problem when performing a basename() on the $_FILES array's 'name' element for uploaded Japanese files, however after testing the bug occurs no matter how you supply the filename.
My PHP environment is running with UTF-8 internal encoding.
The code snippet below illustrates this perfectly.
Reproduce code:
---------------
<?php
// show normal behavior with roman filename
$filename='/myfolder/roman_filename.txt';
echo "The full filename of the romanized file is $filename./n"; // /myfolder/roman_filename.txt
$basename=basename($filename);
echo "The basename of the romanized file is $basename./n"; // /roman_filename.txt
// show behavior with Japanese filename
$filename='/myfolder/��{��̃t�@�C����.txt';
echo "The full filename of the Japanese file is $filename./n"; // /myfolder/��{��̃t�@�C����.txt
$basename=basename($filename);
echo "The basename of the Japanese file is $basename."; // .txt
?>
Expected result:
----------------
The full filename of the romanized file is /myfolder/roman_filename.txt.
The basename of the romanized file is roman_filename.txt.
The full filename of the Japanese file is /myfolder/��{��̃t�@�C����.txt.
The basename of the Japanese file is ��{��̃t�@�C����.txt.
Actual result:
--------------
The full filename of the romanized file is /myfolder/roman_filename.txt.
The basename of the romanized file is roman_filename.txt.
The full filename of the Japanese file is /myfolder/��{��̃t�@�C����.txt.
The basename of the Japanese file is .txt.
Solution1:
$filename = substr ( $down , strrpos ( $down , '/' ) + 1 ) ;
Solution2:
http://hi.baidu.com/janson6788/blog/item/91cdfa6d5c5d15f0431694b2.html
php 有一个 basename() 函数,用来在路径字符串中得到文件名部分,也就最后一个 ”/” 或 “/” 之后的部分,但是在有些平台下,比如我的 fedora 11 + apache ,处理含有中文的路径是会使中文的部分丢失。
一开始想自己写一个函数替代,后来上网找了一个比较好的方法,用的是正则表达式
function get_basename($filename)
{
return preg_replace('/^.+[///]/', '', $filename);
}
这个函数明显强于我自己写的(就不贴出来了:) ),无论是速度还是准确性。
所以以后 php 编程时用到 basename() 函数,最好用这个替代。
Solution3,
http://www.zivee.cn/2010/02/php-basename-dependent-on-locale-setting/
按照网站上找到说法是此函数依赖于区域设置,如果是多字节名称返回为空可以通过 setlocale函数 如下设置
<?php
setlocale(LC_ALL, 'zh_CN.UTF8');
// or any other locale that can handle multibyte characters.
?>
最好是修改服务器的区域设置来整体解决 。