Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Upload utf-8 filename

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

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

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

How to fix that thanks

like image 269
DeLe Avatar asked Aug 13 '13 08:08

DeLe


3 Answers

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

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

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

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

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:

  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.

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

Both choices are deficient:

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

Choice 2: You have to change system code page, and filename characters are limited to code page 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.

like image 71
Inglis Baderson Avatar answered Oct 27 '22 09:10

Inglis Baderson


This meta has no effect:

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

So you have to sned http header Content-Type by yourself:

<?php header("Content-Type: text/html; charset=utf-8"); ?>

ensure that you put this before any html, content or whatever is sent.


Alternatively, accept-charset tag on form should work as weel:

 <form accept-charset="utf-8"> 
like image 30
Zaffy Avatar answered Oct 27 '22 11:10

Zaffy


I am Persian and I have same problem with utf-8 character in my language. I could solve my problem with this code:

$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 sure about vientam language but maybe you can use the same code as above with a few changes:

$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 );
like image 35
rezaSefiddashti Avatar answered Oct 27 '22 09:10

rezaSefiddashti