上传带有韩文文件名的文件不能正确存储到mysql数据库中


Uploading files with korean filename doesn't get stored properly into mysql database

我的上传脚本似乎没有正确地将韩文字符存储到数据库中。

$file_name = $_FILES['Filedata'][ 'name' ];
$newFileName = time() . "-" . $file_name;
$target = $uploaddir . $newFileName;
$source_file_path = $_FILES['Filedata']['tmp_name'];
if( move_uploaded_file( $source_file_path, $target ) ) {
    $sql = "INSERT INTO images(file, album, author) VALUES('".$newFileName."', '".$albumID."', '".$authorID."')";
    $result = mysql_query($sql);
    if(!$result) {
        echo "Could not store into database";
    } else {
        $sql = "UPDATE albums SET `lastdate` = NOW() WHERE id = '".$albumID."'";
        if(mysql_query($sql)) {
            echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$target);
        } else {
            echo mysql_error();
        }
    }
}

奇怪的是,它以正确的名称正确地存储文件,但它将所有不寻常的字符(如"1321088842-ê°ì,¬.jpg")添加到数据库中。数据库设置为utf8_general_ci,所以我不确定问题可能是什么。什么好主意吗?

你需要确保:

  1. 数据库/表/列使用UTF-8
  2. 数据库连接使用UTF-8
  3. web浏览器正在使用UTF-8当你上传文件
  4. 网页浏览器正在使用UTF-8当你显示文件给用户以后
  5. ,你所有的开发工具(例如:phpMyAdmin)使用UTF-8当你查看数据库

如果在任何一点,任何地方,没有使用UTF-8,那么事情将会出现可怕的错误。

请注意,web浏览器通常默认不使用UTF-8。因此,您需要包含一个<meta>标记,指示它使用utf-8,或者手动设置它们来使用它(在我的浏览器中,使用View -> Text Encoding -> utf-8)。

您的php脚本将文件名回显到浏览器,但如果它只是回显文件名而没有<meta>标记,浏览器将尝试将其显示为其他编码,例如latin1.