There exists a bug in PHP which mangles filenames on uploads – see see http://bugs.php.net/bug.php?id=47096 . Therefore I am using a function to convert the filename to proper UTF8.
Heart of the function is:
if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) {
$codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' );
if ( function_exists( 'iconv' ) ) {
$filename = iconv( 'UTF-8', $codepage, $filename );
} elseif ( function_exists( 'mb_convert_encoding' ) ) {
$filename = mb_convert_encoding( $filename, 'UTF-8', $codepage );
}
}
This worked fine with older PHP versions (<5.2). However with recent PHP versions the command
setlocale( LC_CTYPE, 0 )
returns ‘C’ – which is obviously not a Windows code page and so the conversion fails.
Is there a reliable or alternative way to retrieve the current Windows code page in PHP?
Interesting that it worked on older versions of PHP. Without explicitly setting the locale, a return value of
Cindicates that a locale has not been set and the default is being used.You could call
setlocale( LC_CTYPE, '' )which will force the locale to be explicitly set to the default system locale. The return value of calling the method with an empty string should be the newly set locale (as opposed to calling with 0 and expecting the locale to be queried and returned to you without modification).The empty string is a way of explicitly setting the current locale to the default, as opposed to just querying it.