How to solve this encode conflict in php?

0

Personally I have the routine below that makes the listing of files of a directory:

if (is_dir($dir)) {

    $dh = opendir($dir);

    while (($file = readdir($dh)) !== false) {

        if (!in_array($file, array(".", ".."))) {

            $html->NOMEARQ = utf8_encode($file);
            $html->DATAARQ = date("d/m/Y", filemtime($dir . $file));
            $html->TAMANHO = fGetNumero(filesize($dir . $file) / 1000) . " KB";
            $html->LINKARQ = "/arquivos/arquivos.download?p=docs&f=" . utf8_encode($file);

            $html->block("BLOCK_LISTAGEM");

            $qtdarq++;
        }
    }
    closedir($dh);
}

It turns out that my development machine is Windows10 with Apache, and the server is Ubuntu with Apache, on my machine it normally reads the names of files with special characters,

But when I put into production in Ubuntu / Apache I have to remove the utf8_encode function otherwise it will mess up the whole file name.

I know this is because windows and linux treat different names of files, it seems to me that windows uses ANSI and Linux UTF8.

Is there a way to handle this without having to modify my PHP code when putting it on the server?

I can not change the names of the files, remove the accents and the like.

    
asked by anonymous 28.06.2016 / 21:49

3 answers

2

An alternative is to check the server:

if (stripos(php_uname('s'), 'win') === 0) {
   $html->NOMEARQ = utf8_encode($file);
} else {
   $html->NOMEARQ = $file;
}

Another alternative is to force sending header as the colleague suggested in the other response.

    
28.06.2016 / 22:05
1

Have you tried setting the default PHP encoding?

ini_set('default_charset', 'UTF-8')
    
28.06.2016 / 22:15
1

Without modifying php I do not know, but modifying this might solve it. I tested here, in ubuntu / apache, try setting the header:

header('Content-Type: text/html; charset=utf-8');

Or, instead of utf8_encode, do the opposite

utf8_decode($file);

I tested files / directories with special characters and scandir , the latter resulted.

I also saw this solution. If you are in php6, try changing php.ini:

unicode_semantics = On
    
28.06.2016 / 21:56