-
-
-
-
Php Upload code:
- 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). "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"]);
- }
- }
-
Upload a file named "testing data .txt", oh ho, the file is uploaded, but the file name is garbled. The following is an analysis of the causes of garbled php file uploads and provides some reliable solutions. Set: Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/". $ _ FILES ["file"] ["name"]);To: Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload /". iconv ("UTF-8", "gbk", $ _ FILES ["file"] ["name"]);The returned value of the iconv function is false. Check the function manual and find that the second parameter has a special usage. a simple translation is that I can append // Transcoder or // IGNORE after the encoding, the former converts untranslated characters into the nearest character, while the latter directly ignores unconvertible characters. For example: Test: Var_dump (iconv ("UTF-8", "gbk // transcoder", $ _ FILES ["file"] ["name"]); var_dump (iconv ("UTF-8 ", "gbk // IGNORE", $ _ FILES ["file"] ["name"]);Result: bool (false) string (4) ". txt" means that the Chinese text cannot be converted, or even the nearly connected characters do not exist. it seems that the methods introduced on the Internet are not omnipotent. 3. failed to introduce the method on the internet. try again to guess that the system may garbled during the creation of a Chinese file and modify the code: Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/test data .txt ");The result is successfully created with no garbled characters. Not a system problem. Php files are UTF-8 encoded. Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload/test data .txt ");This statement must be UTF-8 encoded, so the uploaded file name must not be UTF-8 encoded. the following statement must be incorrect because the source string itself is not UTF-8 encoded: Iconv ("UTF-8", "gbk // transcoder", $ _ FILES ["file"] ["name"]);Use the function to check the encoding of the source string: $ E = mb_detect_encoding ($ text, array ('utf-8', 'gbk', 'gb2312 '); echo $ e;The result is CP936, that is, the source string encoding is GBK. Test: Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload /". iconv ("gbk", "UTF-8", $ _ FILES ["file"] ["name"]);Solve the problem and there will be no Chinese garbled characters. 4. for other solutions, add the following content to the head tag of an html file: In this way, the encoding remains uniform and no transcoding is required. 5. The iconv function can solve the problem of garbled characters in uploaded Chinese file names. In fact, iconv can solve various garbled characters caused by inconsistent codes. To use the iconv function, check the encoding of the source string unless you have determined the encoding of the source string. Make sure that all codes are encoded in the same way, and use the iconv function as a last resort. Try not to use the Chinese file name as the file name saved on the server. convert the file name to your own file name (even the English file name ). |