PHP rename failed for filename with utf-8 arabic c

2019-02-20 10:49发布

问题:

I have an issue with php rename function, it fail if file name is in Arabic words with spaces.

e.g.

rename(temp/أم كلثوم       ثوار - ثوار.mp3,audio/13408831061.mp3);

No such file or directory in (path)

please note: أم كلثومxxxxxxxxثوار - ثوار.mp3

xxxxxxx here atleast six spaces.

I tried urlencode() but still no result.

Any suggestion!

回答1:

To check that your $file_id is correct, do this on a Unix-like system:

echo "<pre>\n"; // for HTML output only
system("echo -n '$file_id' | od -tx1");

With this value, which should be your reported filename (you can do an echo to check)

$file_id = "\xD9\x83\xD9\x84\xD8\xAB\xD9\x88\xD9\x85 \xD8\xAB\xD9\x88\xD8\xA7\xD8\xB1 - \xD8\xAB\xD9\x88\xD8\xA7\xD8\xB1.mp3";

the output is

0000000 d9 83 d9 84 d8 ab d9 88 d9 85 20 d8 ab d9 88 d8
0000020 a7 d8 b1 20 2d 20 d8 ab d9 88 d8 a7 d8 b1 2e 6d
0000040 70 33
0000042

(as you can see, the sequence ending with a B1 repeats itself, the last 4 bytes are .mp3)

In the directory where the file should be you can issue a

ls -1 | od -tx1

and you will see all filenames separated by 0a which are newlines.

If both outputs match, the problem is somewhere in PHP or in the system (both unlikely), if they don't you will have to chase the problem in your code, starting from the encoding, which must be UTF-8 everywhere.



回答2:

This Sample Should Helps , I Test Bellow Code And It Works In Windows For Arabic/Persian Names:

$newname  = $filename = iconv("utf-8", "cp1256","گچپژ");
echo rename("1.txt", $newname);


标签: php rename