問題描述
我是越南人,我想上傳一個(gè) 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āng)我上傳我在我的電腦上看到的 D: 有一個(gè)類似的文件
but when i upload that i see on my computer D: has a file like
T?an Tá??p Tiáo?ng Viá??t.JPG
如何解決這個(gè)問題,謝謝
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,這是簡(jiǎn)體中文版Windows的默認(rèn)代碼頁.
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);
我不太確定您操作系統(tǒng)的默認(rèn)代碼頁是否為 1258
,您應(yīng)該通過打開命令提示符并輸入命令 chcp
自行檢查.然后將 1258
更改為命令給您的任何內(nèi)容.罷工>
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.
更新
根據(jù)這個(gè)答案,PHP 文件系統(tǒng)函數(shù)似乎只能處理系統(tǒng)代碼頁中的字符.所以你在這里有 2 個(gè)選擇:
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:
將文件名中的字符限制為系統(tǒng)代碼頁 - 在您的情況下,它是
437
.但我很確定代碼頁 437 不包括所有越南語字符.
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.
將您的系統(tǒng)代碼頁更改為越南語:1258
并將文件名轉(zhuǎn)換為 cp1258
.然后文件系統(tǒng)功能應(yīng)該可以工作.
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:您必須更改系統(tǒng)代碼頁,并且文件名字符限制為代碼頁 1258.
Choice 2: You have to change system code page, and filename characters are limited to code page 1258.
更新
如何更改系統(tǒng)代碼頁:
轉(zhuǎn)到控制面板
> Region
> Administrative
> Change system locale
并選擇Vietnamese(越南)
在下拉菜單中.
Go to Control Panel
> Region
> Administrative
> Change system locale
and select Vietnamese(Vietnam)
in the drop down menu.
這篇關(guān)于PHP - 上傳 utf-8 文件名的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!