How do I resolve this conflict in php?

Asked

Viewed 1,509 times

0

Guys I have the routine below that makes the listing of files from 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 filenames with special characters, Action is etc...

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

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

There is a way to handle this without having to modify my PHP code when placing it on the server?

I can’t modify file names, remove accents and the like.

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 the header as the colleague suggested in the other reply.

  • Our guy has to do this check, because only setting with UTF-8 does not work... this Meeting thing sucks, rsrsrs

2

Have you tried this to set the default PHP encoding?

ini_set('default_charset', 'UTF-8')
  • My files are already UTF-8 and yet I put the set for this, but this file listing is giving this problem, I have to set in the development environment and remove in the production environment....

1

Without modifying php, I don’t know, but modifying might solve this. I tested it here, in a 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 with files/directories with special characters and scandir, this last one worked

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

unicode_semantics = On
  • I’m already using the function Encounter and Code, I wanted to do something friendlier, but by the way it will have to be POG same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.