rename not supporting multi-byte characters

2019-06-22 09:19发布

If I write:

rename('php109.tmp','test.jpg');

then it's fine and working.

but if I change it into:

rename('php109.tmp','中文.jpg');

it'll report

"No such file or directory...".

But if the multi-byte characters can be written into database then can be read out, then why it fails when it is renamed ?

4条回答
欢心
2楼-- · 2019-06-22 10:00

Did you try doing a setlocale(LC_ALL, array("es_ES.utf-8","es_ES@euro",'es_ES')); or whatever your country code is, to make sure the locale is set correctly?

If this call does not return something with 'utf-8' in it, it means it failed and will then return the current locale.

查看更多
小情绪 Triste *
3楼-- · 2019-06-22 10:14

File systems do not necessarily use UTF-8. For instance, this is what Wikipedia says about NTFS:

NTFS allows any sequence of 16-bit values for name encoding (file names, stream names, index names, etc.). This means UTF-16 codepoints are supported, but the file system does not check whether a sequence is valid UTF-16 (it allows any sequence of short values, not restricted to those in the Unicode standard).

You might need to use iconv() to convert between charsets.

查看更多
叼着烟拽天下
4楼-- · 2019-06-22 10:15

I'm almost sure that the mbstring has nothing to do with this specific problem, I think the problem here relies on the encoding of your .php file.

Try changing the encoding of the file to UTF-8 (no BOM!) in your code editor.

查看更多
狗以群分
5楼-- · 2019-06-22 10:16

This Sample Should Helps , You Should Find Your Language Code Page And Replace With That. I Test Bellow Code And It Works In Windows For Arabic/Persian Names:

$newname = iconv("utf-8", "cp1256","گچپژ");
echo rename("1.txt", $newname);
查看更多
登录 后发表回答