PHP upload file is the most basic technical point, but there are many problems to be solved, this is not, upload Chinese files, file name into garbled.
Here is the problem code, very simple:
1. Problem code
HTML section:
Copy Code code as follows:
<body>
<form action= "upload_file.php" method= "POST"
Enctype= "Multipart/form-data" >
<label for= "File" >Filename:</label>
<input type= "File" name= "file" id= "file"/>
<br/>
<input type= "Submit" name= "submit" value= "Submit"/>
</form>
</body>
PHP section:
Copy Code code as follows:
<?php
if ($_files["file"] ["error"] > 0)
{
echo "Return Code:". $_files["File" ["Error"]. "<br/>";
}else
{
echo "Upload:". $_files["File" ["Name"]. "<br/>";
echo "Type:". $_files["File" ["type"]. "<br/>";
echo "Size:". ($_files["File"] ["size"]/1024). "Kb<br/>";
echo "Temp file:". $_files["File" ["Tmp_name"]. "<br/>";
if (file_exists ("upload/". $_files["File" ["Name"])
{
echo $_files["File" ["Name"]. "already exists."
}
Else
{
Move_uploaded_file ($_files["file"] ["Tmp_name"],
"Upload/". $_files["File" ["name"]);
}
}
uploaded a file named "Test data. txt" files, oh ho, the file is passed up, but the file name is garbled.
2. Preliminary examination
Search the solution online to
Copy Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]);
Change into
Copy Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". Iconv ("UTF-8", "GBK", $_files["file" ["Name"]));
The result found that the ICONV function returned a value of false.
Check the function manual, found that the second parameter has a special use, simple translation is that I can append//translit or//ignore after the code, the former will not be translated characters into the closest characters, the latter is to directly ignore the characters can not be converted.
Try this:
Copy Code code as follows:
Var_dump (Iconv ("UTF-8", "Gbk//translit", $_files["file"] ["name"]);
Var_dump (Iconv ("UTF-8", "Gbk//ignore", $_files["file"] ["name"]);
Results:
BOOL (FALSE) string (4) ". txt"
In other words, Chinese can not be transformed, even close to the characters are not, it seems that the method of online introduction is not omnipotent.
3. Online Introduction method failed, try again
Guess, my system may be in the creation of Chinese files will be garbled, so I rewrite the code:
Copy Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/test data. txt");
Results created successfully, no garbled ... That means it's not a system problem.
Think, my php file itself is UTF8 encoded, then
Copy Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/test data. txt");
This statement is definitely using UTF8 encoding, then the file name uploaded before must not be UTF8 encoded, then the following statement is certainly wrong, because the source string itself is not UTF8 encoded:
Copy Code code as follows:
Iconv ("UTF-8", "Gbk//translit", $_files["file" ["name"]);
To check the encoding of the source string using a function:
Copy Code code as follows:
$e =mb_detect_encoding ($text, Array (' UTF-8 ', ' GBK ', ' gb2312 '));
Echo $e;
The result is CP936, which is that the source string encoding is GBK.
Try
Copy Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". Iconv ("GBK", "UTF-8", $_files["file" ["Name"]));
Problem solved, no longer garbled
4. Another solution
In fact, there is also a solution, which is to add in the middle of the head tag of the HTML file
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
So that the code is unified and there's no need to turn the code.
5. Here is the conclusion
Using the Iconv function can solve the problem of uploading Chinese file name garbled, in fact, iconv can solve a variety of problems caused by the encoding is not uniform.
Use the Iconv function to first check the encoding of the source string, unless you have determined the encoding of the source string.
Try to make sure that all code is coded consistently, using the ICONV function only as a last resort.
Spit, try not to use the Chinese file name as the name of the file saved on the server, convert the file name to its own file name (even if the English file name is converted).