1

Using file field to upload image which has utf-8 filename, but when it is uploaded to site filename is not saving in utf-8 format.But original name is saved as link in database making it not found when accessed.

AddDefaultCharset utf-8 is added to httpd.conf and server restarted before testing.

original filename - noëlle_châtelet.jpg(same is saved in database)
saved filename -noëlle_châtelet.jpg

enter image description here

2
  • Is that on a windows server? I have exactly the same problem on windows server, while it is working correctly on linux server.
    – Elin Y.
    Commented Mar 18, 2015 at 9:05
  • Ok, I've read the answer you posted. It's seems like you are exactly in the same situation. Have you found any better solution by now, other than hacking the core?
    – Elin Y.
    Commented Mar 18, 2015 at 9:11

2 Answers 2

1

According detailed explanation at https://bugs.php.net/bug.php?id=47096 temporary solution would be, explicitly converting filename encoding using iconv().Now it raising new warning message(shall I have to open another issue thread for this?)

Warning: filesize() [function.filesize]: stat failed for public://noëlle_châtelet12.jpg in file_save() (line 566 of C:\server\www\drup_dlist_mys\includes\file.inc)

temporary solution until it is fixed in php move_uploaded_file() function. In file.inc I added the line in drupal_move_uploaded_file function block

+$uri= iconv("UTF-8","ISO-8859-1//TRANSLIT", $uri);
$result = @move_uploaded_file($filename, $uri);
// PHP's move_uploaded_file() does not properly support streams if safe_mode
// or open_basedir are enabled so if the move failed, try finding a real path
// and retry the move operation.
.
.

This is only happening in WAMP and working fine in linux. I don't know how this new iconv statement will affect in unix/linux environment.Can it be possible to check whether the current locale supports for utf-8 just before iconv() statement?.

1

The only thing AddDefaultCharset does is to add charset=utf-8 to the HTTP header Content-type if the previous part is missing. Nothing to see with uploads or downloads.

On a Windows system, PHP functions relative to file system do not handle UTF-8 but ANSI code page (CP1252 for French) as PHP use ANSI functions and not Unicode functions (UTF-16) of Windows API.

IMO, in general, the best way to handle uploads is to generate unique file names and ignore client file names or, at least, ignore bytes > 0x7F.

$filename = filter_var($_FILES['upload']['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);

Can it be possible to check whether the current locale supports for utf-8 just before iconv() statement?

You can use setlocale (to get current locale FALSE !== strpos(setlocale(LC_CTYPE, '0'), 'UTF-8') and/or returns FALSE if it is unavaible if (!setlocale(LC_CTYPE, 'fr_FR.UTF-8'))).

UPDATE: as of PHP 7.1.0, PHP can now use UTF-8 encoded paths on Windows (set internal_encoding to UTF-8, for example - should be the case by default).

2
  • what do you think who is culprit here windows filesystem or php's file handling function. If it is php we can expect patch in next release like this and if it is windows I dont know will they provide any configuration to make utf-8 as default locale :( . +1 for check locale
    – kiranking
    Commented Sep 14, 2013 at 19:02
  • PHP is guilty as long as it won't 1) associate a charset to "strings" and assume needed conversions between the different charsets ; 2) use Unicode version of C functions of Windows API. The patch, you link, do its best but is really unsufficient [regarding Unicode]. Also, Nicolas Grekas suggested a "trick" via COM object (see end of the PR you linked - that I have not tested).
    – julp
    Commented Sep 14, 2013 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.