PHP - 上传 utf-8 文件名

我是越南人,我想上传一个 utf-8 文件名

I'm Vietnamese and i want to upload a utf-8 filename like

Tên Tệp Tiếng Việt.JPG

这是我的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>utf-8</title> 
</head> 

<body> 
<?php
    if(isset($_POST["submit"])) { 
       if($_FILES["upload"]["error"] > 0 ) echo "FILE ERROR!"; 
       else 
         { 
           $base_dir = "D:/"; 
           $fn = $_FILES["upload"]["name"]; 

           $fn2 = $base_dir.$fn;

           move_uploaded_file($_FILES["upload"]["tmp_name"],$fn2); 
         } 
     } 
?> 
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
  <input type="file" name="upload" id="upload" /> 
  <input type="submit" name="submit" id="submit" value="Send" /> 
</form> 
</body> 
</html> 

但是当我上传我在我的电脑上看到的 D: 有一个类似的文件

but when i upload that i see on my computer D: has a file like

Tên Tệp Tiếng Việt.JPG

如何解决这个问题,谢谢

How to fix that thanks

推荐答案

我使用的是 Windows 8 中文版,我遇到了类似的问题:

I'm on Windows 8 chinese version, and I deal with similar problem with this:

$filename = iconv("utf-8", "cp936", $filename);

cp 代表 代码页cp936 代表 代码页936,这是简体中文版Windows的默认代码页.

cp stands for Code page and cp936 stands for Code page 936, which is the default code page of simplified chinese version of Windows.

<打击>所以我想也许你的问题可以用类似的方式解决:

So I think maybe your problem could be solved in a similar way:

$fn2 = iconv("UTF-8","cp1258", $base_dir.$fn);

我不太确定您操作系统的默认代码页是否为 1258,您应该通过打开命令提示符并输入命令 chcp 自行检查.然后将 1258 更改为命令给您的任何内容.

I'm not quite sure whether the default code page of your OS is 1258 or not, you should check it yourself by opening command prompt and type in command chcp. Then change 1258 to whatever the command give you.

更新

根据这个答案,PHP 文件系统函数似乎只能处理系统代码页中的字符.所以你在这里有 2 个选择:

It seems that PHP filesystem functions can only handle characters that are in system codepage, according to this answer. So you have 2 choices here:

  1. 将文件名中的字符限制为系统代码页 - 在您的情况下,它是 437.但我很确定代码页 437 不包括所有越南语字符.

  1. Limit the characters in the filename to system codepage - in your case, it's 437. But I'm pretty sure that code page 437 does not include all the vietnamese characters.

将您的系统代码页更改为越南语:1258 并将文件名转换为 cp1258.然后文件系统功能应该可以工作.

Change your system codepage to the vietnamese one: 1258 and convert the filename to cp1258. Then the filesystem functions should work.

两种选择都有缺陷:

选择 1:您不能再使用越南语字符,这不是您想要的.

Choice 1: You can't use vietnamese characters anymore, which is not what you want.

选择 2:您必须更改系统代码页,并且文件名字符限制为代码页 1258.

Choice 2: You have to change system code page, and filename characters are limited to code page 1258.

更新

如何更改系统代码页:

转到控制面板 > Region > Administrative > Change system locale 并选择Vietnamese(越南) 在下拉菜单中.

Go to Control Panel > Region > Administrative > Change system locale and select Vietnamese(Vietnam) in the drop down menu.

相关文章