0

Why does the following code mistakenly renames 1.txt to 一.txt.txt and not 一.txt?

rename('1.txt','一.txt');
dev4life
  • 10,785
  • 6
  • 60
  • 73

2 Answers2

1

Mostly because of encoding mismatch. You script shall use right encoding and this encoding have to match file system encoding. If one of these mismatch (usually script's) then you get the described effect. Ensure you use the same encoding or replace your "in-lined" name (FYI your target file name looks here as "-.txt" where "-" is long dash) with numeric entity.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • how can I set the right enconding in my script in order to start renaming files with Japanese characters? (by the way, the "long dash" is a Japanese character and not a regular dash) – dev4life Aug 10 '15 at 15:13
  • The encoding of the script file is UTF-8, shouldnt that work? – dev4life Aug 10 '15 at 15:21
1

Ah the joy of mixed encoding. The mistake could be anywhere, in your file system, in your network encoding, in your shell, in your editor, in your file manager, even in the terminal not interpreting the characters correctly, ...

Please ensure that everybody speaks the same character set. This is usually fixed by running with the correct environment variables in place. Please be aware of the fact the PHP doesn't really do anything with the file name's characters, so as long as you're not using string functions to manipulate strings (which are, unfortunately, still only compatible with single byte strings) you shouldn't be in much trouble from PHP.

Gerard van Helden
  • 1,601
  • 10
  • 13