上传到服务器时损坏的西里尔文件名 [英] Broken Cyrillic file name when uploading to server

查看:128
本文介绍了上传到服务器时损坏的西里尔文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个PHP功能:

I 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');
}    

其中$ fn =нова-категория
但是当我将重命名的文件上传到服务器 - 图像名称已损坏,如下所示:$ b $bновакаС,РμРіРѕСЂРёСЏ.png

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

有趣的是如果我试图访问服务器上的图像:site.com/images/pc/нова-категория.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. 当您使用ftp客户端浏览ftp时,您会看到ANSI编码的名称(单字节编码)。在这种情况下,РЅРѕРІР°-РєР°С,РμРіРѕСЂРёСЏ.png实际上是UTF-8(双字节)编码нова-категория.png

  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

当您将文件上传到网络服务器时,浏览器会将文件名中的非unicode符号转换为UTF-8( нова-категория.png成为РЅРѕРІР°Р,Р°С,РμРіРѕСЂРёСЏ.png

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

当您请求site.com/images/pc/нова-категория.png浏览器再次将非unicode符号转换为UTF-8且服务器实际上寻找РЅРѕРІР°-РєР°С,РμРіРѕСЂРёСЏ.png(采用ASCI编码)。

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).

因此,如果你想在ftp-client中看到普通名称,你应该将它们转换为你的原生编码

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']){
...

但在这种情况下,您的文件URL会出现问题。
要将正确的URL写入ANSI编码的名称,您应该使用此PHP代码:

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.

这篇关于上传到服务器时损坏的西里尔文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆