PHP rename failed for filename with utf-8 arabic charset string

后端 未结 2 1352
Happy的楠姐
Happy的楠姐 2021-01-15 02:09

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/         


        
2条回答
  •  星月不相逢
    2021-01-15 02:33

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

    echo "
    \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.

提交回复
热议问题