0

I'm developing a french website and users in some cases needs to upload files(pdfs) and I want to save this files with their original filenames, when I try to do this, I found that PHP can't save the file with it's original filename if this filename contain letters such (é,ç,à,...) so I thought that I should replace all those french chars by their English equivalent (é->e, à a,...) I found this answer but it won't work for me ... and this is the code that doesn't work :

$unwanted_array = array('Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
                        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
                        'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
                        'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
                        'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );

$ori_fn=strtolower(basename($_FILES['userfile']['name']));
$mod_fn = strtr($ori_fn, $unwanted_array);

echo "Before : ".$ori_fn."<br>";
echo "After : ".$mod_fn."<br>";//$mod_fn same as $ori_fn nothing changed !

... this not work just when I retrieve filename string from $_FILES as the code above, but when I test with simple string as strtr("référence",$unwanted_array); then it's work normaly !

1 - What is the problem ?

2- What is the solution ?

3 - Is there better way to normalize filenames ?!

Note I mean by doesn't work : strtr doesn't change anything in received string($_FILES['userfile']['name']) !

Community
  • 1
  • 1
Az.Youness
  • 2,167
  • 1
  • 24
  • 33
  • I'm not sure if I understand your comment correctly, but it seems that it's bad idea to store files with it's original name right ! – Az.Youness Jan 08 '16 at 16:41

1 Answers1

1

strtolower doesn't work with Unicode strings, you should use mb_strtolower :

$ori_fn = mb_strtolower(basename($_FILES['userfile']['name']), 'UTF-8');
$mod_fn = strtr($ori_fn, $unwanted_array);

However, PHP has the iconv function for this:

$ori_fn = strtolower(basename($_FILES['userfile']['name']));
$mod_fn = iconv('UTF8', 'ASCII//TRANSLIT', $ori_fn);
Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32