douzhen5158 2016-01-08 16:15
浏览 18
已采纳

'Normalize'上传的文件名为php

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']) !

  • 写回答

1条回答 默认 最新

  • doumu6941 2016-01-08 16:32
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF RichTextBox格式化大量文本卡顿如何解决
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决