PHP - 上传 utf-8 文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18204364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Upload utf-8 filename
提问by DeLe
I'm Vietnamese and i want to upload a utf-8 filename like
我是越南人,我想上传一个 utf-8 文件名,例如
Tên T?p Ti?ng Vi?t.JPG
Tên T?p Ti?ng Vi?t.JPG
Here is my code
这是我的代码
<!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>
but when i upload that i see on my computer D:\ has a file like
但是当我上传我在我的电脑上看到的 D:\ 有一个像
T?an Tá??p Tiáo?ng Viá??t.JPG
T?an Tá??p Tiáo?ng Viá??t.JPG
How to fix that thanks
怎么解决谢谢
采纳答案by Inglis Baderson
I'm on Windows 8 chinese version, and I deal with similar problem with this:
我在 Windows 8 中文版上,我处理了类似的问题:
$filename = iconv("utf-8", "cp936", $filename);
cp
stands for Code page
and cp936
stands for Code page 936, which is the default code page of simplified chinese version of Windows.
cp
代表Code page
和cp936
代表代码页936,这是的Windows简体china版的默认代码页。
So I think maybe your problem could be solved in a similar way:
所以我想也许你的问题可以用类似的方式解决:
$fn2 = iconv("UTF-8","cp1258", $base_dir.$fn);
$fn2 = iconv("UTF-8","cp1258", $base_dir.$fn);
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.
我不太确定您的操作系统的默认代码页是否是1258
,您应该通过打开命令提示符并输入 command 来自行检查chcp
。然后更改1258
为命令给您的任何内容。
UPDATE
更新
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:
根据这个答案,PHP 文件系统函数似乎只能处理系统代码页中的字符。所以你在这里有 2 个选择:
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.Change your system codepage to the vietnamese one:
1258
and convert the filename tocp1258
. Then the filesystem functions should work.
将文件名中的字符限制为系统代码页 - 在您的情况下,它是
437
. 但我很确定代码页 437 不包括所有越南语字符。将您的系统代码页更改为越南语:
1258
并将文件名转换为cp1258
. 然后文件系统功能应该可以工作。
Both choices are deficient:
两种选择都有缺陷:
Choice 1: You can't use vietnamese characters anymore, which is not what you want.
选择1:你不能再使用越南语字符,这不是你想要的。
Choice 2: You have to change system code page, and filename characters are limited to code page 1258.
选择 2:您必须更改系统代码页,并且文件名字符限制为代码页 1258。
UPDATE
更新
How to change system code page:
如何更改系统代码页:
Go to Control Panel
> Region
> Administrative
> Change system locale
and select Vietnamese(Vietnam)
in the drop down menu.
转到Control Panel
> Region
> Administrative
>Change system locale
并Vietnamese(Vietnam)
在下拉菜单中选择。
回答by Zaffy
This meta
has no effect:
这meta
没有效果:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
because the web server has already sent Content-Type
header, and thus decided what the encoding will be. Web browsers send forms in the same encoding. The meta
is useful when user is off-line.
因为网络服务器已经发送了Content-Type
标头,因此决定了编码是什么。Web 浏览器以相同的编码发送表单。这meta
在用户离线时很有用。
So you have to sned http header Content-Type
by yourself:
所以你必须自己sned http标头Content-Type
:
<?php header("Content-Type: text/html; charset=utf-8"); ?>
ensure that you put this before any html, content or whatever is sent.
确保将其放在任何 html、内容或发送的任何内容之前。
Alternatively, accept-charset
tag on form
should work as weel:
或者,accept-charset
标记form
应该可以正常工作:
<form accept-charset="utf-8">
回答by rezaSefiddashti
I am Persian and I have same problem with utf-8 character in my language. I could solve my problem with this code:
我是波斯人,我的语言中 utf-8 字符也有同样的问题。我可以用这段代码解决我的问题:
$fn = $_FILES["upload"]["name"]; // name of file have some utf-8 characters
$name=iconv('utf-8','windows-1256', str_replace('?', '?', $fn));
move_uploaded_file($_FILES["upload"]["tmp_name"],$name );
I am not sureabout vientam language but maybe you can use the same code as above with a few changes:
我不确定vientam 语言,但也许您可以使用与上面相同的代码并进行一些更改:
$fn = $_FILES["upload"]["name"]; // name of file have some utf-8 characters
$name=iconv('utf-8','cp936', $fn);
move_uploaded_file($_FILES["upload"]["tmp_name"],$name );
回答by T.Todua
The only solutionI have found so far.. (2014 year):
到目前为止我找到的唯一解决方案..(2014年):
1) I dont store files on my FTP in UTF-8 string. Instead, i use this function, to rename the uploaded files:
1) 我不在我的 FTP 上以UTF-8 字符串存储文件。相反,我使用这个函数来重命名上传的文件:
<?php
// use your custom function.. for example, utf8_encode
$newname = utf8_encode($_FILES["uploadFile"]["name"]);
move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $newname);
?>
2) Now you can rename (or etc) $newname
;
2)现在您可以重命名(或等)$newname
;
回答by Danoosh
Sorry! your question is about file name.
对不起!你的问题是关于文件名。
You must save your file name with iconv but read without this.
你必须用 iconv 保存你的文件名,但没有这个就可以阅读。
for saving:
为了节省:
<?php
$name = $_FILES['photo']['name'];
$unicode = iconv('windows-1256', 'utf-8', $name);
move_uploaded_file($_FILES['photo']['tmp_name'], 'photos/' . $name);
mysql_query("INSERT INTO `photos` (`filename`) VALUES ('{$unicode}')");
?>
for reading:
阅读:
<?php
$images = mysql_query('SELECT * FROM `photos`');
if($images && mysql_num_rows($images) > 0) {
while($image = mysql_fetch_assoc($images)) {
$name = iconv('utf-8', 'windows-1256', $image['filename']);
echo '<img src="photos/' . $name . '"/>';
}
mysql_free_result($images);
}?>
回答by iproger
回答by Saurabh Chandra Patel
function convToUtf8($str)
{
if( mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")!="UTF-8" )
{
return iconv("gbk","utf-8",$str);
}
else
{
return $str;
}
}
$filename= convToUtf8($filename) ;
回答by Mahmoud Samy
try this
尝试这个
$imgname = $_FILES['img'] ['name'] ;
$imgsize = $_FILES['img'] ['size'] ;
$imgtmpname = $_FILES['img'] ['tmp_name'] ;
$imgtype = $_FILES['img'] ['type'] ;
$size = 1024;
$imgtypes = array('image/jpeg','image/gif','image/png');
$folder = "up";
if(empty($imgname)){
echo "Shose ur photo";
}else if(!in_array($imgtype,$imgtypes)){
echo "this photo type is not avalable";
}else if($imgsize > $size){
echo "this photo is dig than 6 MB";
}else if($imgwidth > 5000){
echo "the file is to big";
}
else{
move_uploaded_file($imgtmpname, $folder, $filename);
}
回答by guihknx
You can use this
你可以用这个
$fn2 = basename($_FILES['upload']['name']);