Presumably a lot of friends in the UTF8 encoding php development upload function, will encounter such a problem, that is, uploading Chinese file name files, filenames will become garbled, in fact, we can use the Iconv function to re-encode the file name to solve the problem
PHP files use UTF-8 encoding, if there is no wrong guess Apache processing should be GBK. To understand this problem, to find the relevant tutorial, simply found the iconv this function.
Function Prototypes: String Iconv (String in_charset, String out_charset, String str)
Examples of use: $content = Iconv ("GBK", "UTF-8", $content);
The purpose of this example is to convert $content from GBK to UTF-8 encoding.
Garbled problem key code:
Copy the code code as follows:
$name =iconv( "UTF-8" , "gb2312" , $name ); |
In addition to this to solve the problem of uploading files in Chinese garbled, we can also rename the upload file.
Cases
Copy the code code as follows:
$sFileName = "sda.php";
$sOriginalFileName = $sFileName;
$sExtension = S Str ($sFileName, (Strrpos ($sFileName, '. ') + 1));//Find extension
$sExtension = Strtolower ($sExtension);
$sFileName = Date ("Ymdhis"). Rand (100, 200). ".". $sExtension; This is our new file name, all the numbers will not be garbled oh.
Here are some additions:
PHP upload Chinese file name garbled solution
Copy the code code as follows:
$filepath = "upload/";
$name = $filepath. $_files["Upfile" ["Name"];
while (File_exists ($name)) {
$temp =explode (".", $name);//Split string
$name = $temp [0]. " 0″. ".". $temp [1];//main file name plus 0
}
Copy the code code as follows:
The Iconv () function is the key
if (Move_uploaded_file ($_files["Upfile"] ["Tmp_name"],iconv ("Utf-8″," gb2312 ", $name))) {//processing ...}
My PHP code is UTF-8, which may be because the operating system is GBK!
Note: My server is Windows XP, Apache, and the estimated XP character set is GBK. Because my PHP code is saved in utf-8 format, in the name of the file name will be garbled, so you can use the Iconv () function to convert the original utf-8 format file name to GBK format.
PHP UTF8 code to upload Chinese file name garbled solution method
Presumably a lot of friends in the UTF8 encoding php development upload function, will encounter such a problem, that is, uploading Chinese file name files, filenames will become garbled, we can use the Iconv function to re-encode the file name to solve the problem.
Copy the code code as follows:
header( "Content-Type:text/html;charset=utf-8" ); |
$submit = $_POST [ ‘submit‘ ]; |
if (isset( $submit ) && trim( $submit ) != ‘‘ ){ |
if (isset( $file [ ‘tmp_name‘ ])){ |
$name = iconv( ‘utf-8‘ , ‘gb2312‘ , $file [ ‘name‘ ]); //利用Iconv函数对文件名进行重新编码 |
if (move_uploaded_file( $file [ ‘tmp_name‘ ], $name )){ |
How to solve the garbled file name in PHP files