PHP scandir() and htmlentities(): issues with charset and/or special characters

前端 未结 1 1938
误落风尘
误落风尘 2021-01-11 19:12

I am using jqueryFileTree to show a directory listing on the server with download links to the files in the directory. Recently I\'ve run into an issue with files which cont

相关标签:
1条回答
  • 2021-01-11 20:08

    My best guess is that the filename itself isn't using UTF-8. Or at least scandir() isn't picking it up like that.

    Maybe mb_detect_encoding() can shed some light?

    var_dump(mb_detect_encoding($filename));
    

    If not, try to guess the encoding (CP1252 or ISO-8859-1 would be my first guess) and convert it to UTF-8, see if the output is valid:

    var_dump(mb_convert_encoding($filename, 'UTF-8', 'Windows-1252'));
    var_dump(mb_convert_encoding($filename, 'UTF-8', 'ISO-8859-1'));
    var_dump(mb_convert_encoding($filename, 'UTF-8', 'ISO-8859-15'));
    

    Or using iconv():

    var_dump(iconv('WINDOWS-1252', 'UTF-8', $filename));
    var_dump(iconv('ISO-8859-1',   'UTF-8', $filename));
    var_dump(iconv('ISO-8859-15',  'UTF-8', $filename));
    

    Then when you've figured out which encoding is actually used, your code should look somewhat like this (assuming CP1252):

    $filename = htmlentities(mb_convert_encoding($filename, 'UTF-8', 'Windows-1252'), ENT_QUOTES, 'UTF-8');
    
    0 讨论(0)
提交回复
热议问题