PHP upload file is the most basic of a technical point, but in-depth there are many problems to be solved, this does not, after uploading the Chinese file, the file name becomes garbled.
Here is the problem code, very simple:
1. Problem code
HTML section:
Copy the Code code as follows:
PHP section:
Copy the Code code as follows:
<?php
if ($_files["file" ["error"] > 0)
{
echo "Return Code:". $_files["File" ["Error"]. "
";
}else
{
echo "Upload:". $_files["File" ["Name"]. "
";
echo "Type:". $_files["File" ["type"]. "
";
echo "Size:". ($_files["File" ["Size"]/1024). The Kb
";
echo "Temp file:". $_files["File" ["Tmp_name"]. "
";
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" file, oh ho, the file is passed up, but the file name is garbled.
2. Preliminary examination
Search the solution online to
Copy the Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]);
Change into
Copy the Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". Iconv ("UTF-8", "GBK", $_files["file" ["Name"]));
The result is that the return value of the Iconv function is false.
Check the function manual, found that the second parameter has a special use, simple translation is I can add//translit or//ignore after the code, the former will be unable to translate the characters to the nearest character, the latter is directly ignore the characters can not be converted.
Try it:
Copy the 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"
That is, the Chinese can not be converted, and 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, perhaps my system in the creation of Chinese files will be garbled, so I rewrite the code:
Copy the Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/test data. txt");
Results created successfully, no garbled ... This is not a system problem.
Imagine that my php file itself is UTF8 encoded, then
Copy the Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/test data. txt");
This statement is sure to use UTF8 encoding, then the file name that was uploaded is definitely not UTF8 encoded, then the following statement is definitely wrong, because the source string itself is not UTF8 encoded:
Copy the Code code as follows:
Iconv ("UTF-8", "Gbk//translit", $_files["file" ["name"]);
Use the function to check the encoding of the source string:
Copy the Code code as follows:
$e =mb_detect_encoding ($text, Array (' UTF-8 ', ' GBK ', ' gb2312 '));
Echo $e;
The result is CP936, which is the source string encoding is GBK.
Try it.
Copy the Code code as follows:
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". Iconv ("GBK", "UTF-8", $_files["file" ["Name"]));
Problem solving, no longer garbled
4. Another solution
There is actually a workaround, which is to add the HTML file to the head tag
Copy the Code code as follows:
So that the code remains unified, there is no need to transcode
5. The following is the conclusion
Using the Iconv function can solve the problem of uploading Chinese file names garbled, in fact, iconv can solve a variety of codes due to the lack of uniformity caused by garbled problems.
Use the Iconv function to check the encoding of the source string first, unless you have determined the encoding of the source string.
Try to ensure that all code is coded consistently, using the ICONV function only as a last resort.
Spit it out, try not to use the Chinese file name as the file name that is saved on the server, please convert the file name to your own filename (even if the English file name is translated).