文件名的第一个字母不好

时间:2014-08-08 05:36:36

标签: php

我使用UTF-8字母。它工作正常,但如果文件中的第一个字母是“č”,“ž”,“š”......不能正常工作。 文件名是:čačača.doc 我有这段代码:

$name1 = $_FILES["file"]["name"];  //here is ča ča ča.doc
$ext = pathinfo($name1, PATHINFO_EXTENSION); //here is doc
$name = basename($name1, $ext); //here is "a ča ča." missing first letter

这不适用于这里第一个“č,š,ž,đ...”

1 个答案:

答案 0 :(得分:1)

PHP iconv()

尝试再次将$name1编码为其他编码 Windows-1252

//encode to windows-1252 to save to the filesystem
$name1 = $_FILES["file"]["name"];  //here is ča ča ča.doc
$encoded_filename = iconv("UTF-8","Windows-1252//IGNORE",$name1);

CP858

$name1 = $_FILES["file"]["name"];  //here is ča ča ča.doc
$encoded_filename = iconv("UTF-8", "CP858//IGNORE", $name1)

经过30分钟后,我发现,你可以试试这个:

$search = array('š','á','ž','í','ě','é','ř','ň','ý','č',' ');
$replace = array('s','a','z','i','e','e','r','n','y','c','-');

$code_encoding = "UTF-8"; // this is my guess, but put whatever is yours
$os_encoding = "CP-1250"; // this is my guess, but put whatever is yours

$name1 = $_FILES["file"]["name"];
$name1 = iconv($os_encoding , $code_encoding, $name1); // convert before replace
$name1 = str_replace($search, $replace, $name1); 
  

参考:https://stackoverflow.com/a/1767011/2236219