Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opendir and readdir returns utf8

Why does this return utf8 chars on one server but not on another? Some filenames contain unicode chars like æ ø and å..

In some environments utf8 chars are printed and in some iso chars are printed

header('content-type: text/plain');
$handle = opendir("./dir");
while($readdir = readdir($handle)){
    echo "$readdir\n";
}

output Content-Type:text/plain;charset=UTF-8

Retursvar 2 med fejl p� debiteringsniveau.xml
Retursvar 2 med fejl på debiteringsniveau.xml

The same locales are installed on both systems (UTF8)

dpkg-reconfigure locales
like image 829
clarkk Avatar asked Mar 07 '16 17:03

clarkk


1 Answers

You may find that you web server is running under a different locale on each system.

To determine this run the following php via your webserver:

<?php
system("locale");

Chances are the webserver returning the correct characters is running in either a 'utf8' locale or 'C'.

The locale that the webserver uses can be set in different places. Either by using the system wide locale - or a service specific one. You probably need to investigate the reason for any difference and decide whether to update your system wide locale or just the webserver.

On Debian, the system wide locale change be changed using

dpkg-reconfigure locales

And following the prompts.

Alternatively, for service specific locales. Set the 'LANG' environment variable to the one you require in the service init script prior to starting the service. There's probably a service specific config file you could also search out to persist the change in event of webserver upgrades.

like image 150
Steve E. Avatar answered Sep 22 '22 06:09

Steve E.