Broken Cyrillic file name when uploading to server

2019-08-26 01:44发布

enter image description hereI have this php function:

function upload_file($f,$fn){
switch($f['type']){
case 'image/jpeg':$image = imagecreatefromjpeg($f['tmp_name']);break;
case 'image/png':case 'image/x-png':move_uploaded_file($f['tmp_name'],'../images/pc/'.$fn.'.png');break;
case 'image/pjpeg':$image = imagecreatefromjpeg($f['tmp_name']);break;
echo $f['type'],'<br />';
}
if(!empty($image)) imagejpeg($image,'../images/pc/'.$fn.'.png');
}    

where $fn = "нова-категория" but when I upload the renamed file to server - the image name is broken and looks like this: РЅРѕРІР°-категория.png

The interesting thing is that if I try to visit the image on server: site.com/images/pc/нова-категория.png => I can see the image.. Can you give me an idea what brakes the image name to look normal?

1条回答
Rolldiameter
2楼-- · 2019-08-26 01:56
  1. When you browse ftp with ftp client you see ANSI-encoded names (single-byte encodings). In this case РЅРѕРІР°-категория.png is actually UTF-8 (double-byte) encoded нова-категория.png

  2. When you upload file to web server, browser convert non unicode symbols in file name to UTF-8 (нова-категория.png becomes РЅРѕРІР°-категория.png)

  3. When you request site.com/images/pc/нова-категория.png browser again convert non unicode symbols to UTF-8 and server actually looks for РЅРѕРІР°-категория.png (in ASCI-encoding).

So if you want to see "normal" names in ftp-client, you should convert them to your native encoding

function upload_file($f,$fn){
$fn=iconv("UTF-8","Windows-1251",$fn);
switch($f['type']){
...

But in this case you'll have problems with URLs to your files. To write correct URL to ANSI-encoded names you should use this php code:

echo "site.com/images/pc/".rawurlencode("нова-категория.txt");

The way you should handle file names is depends on your use of that files. But I don't recommend you to convert them. If you have problem, I thinks its not in "broken" names.

查看更多
登录 后发表回答