【问题标题】:Uploading files with korean filename doesn't get stored properly into mysql database使用韩文文件名上传文件未正确存储到 mysql 数据库中
【发布时间】:2011-12-27 13:32:33
【问题描述】:

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

$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,所以我不确定问题可能是什么。有什么想法吗?

【问题讨论】:

  • 如果从 php 中回显它会打印什么?如果它仍然是乱码,我想您必须在更新数据库之前将其编码为 php 中的 UTF-8。

标签: php mysql upload cjk


【解决方案1】:

您需要确保:

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

如果在此过程中的任何地方有任何一点不使用 UTF-8,那么事情就会大错特错。

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

您的 php 脚本正在向浏览器回显文件名,但如果它只是回显没有 <meta> 标记的文件名,浏览器将尝试将其显示为其他编码,可能是 latin1。

【讨论】:

  • +1 表示 #1。对于初学者来说,这是一个容易犯的错误。 My dbase is UTF-8! 好吧,这还不够! But my table is also UTF-8! ...还不够! Oh, the column and field are now UTF-8 too! ...到达那里!
  • 谢谢!问题出在数据库连接上。因为这个过程是通过javascript/php交互完成的,所以忘记在php文件上设置字符集了。
猜你喜欢
  • 2017-08-09
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 2012-07-25
  • 2017-01-15
  • 2019-09-21
  • 2021-07-15
相关资源
最近更新 更多