Robert Lee
Robert Lee

Reputation: 105

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

My upload script doesn't seem to store Korean characters into a database properly.

$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();
        }
    }
}

The weird thing is, it stores the file properly with the right name, but it adds all the unusual characters like "1321088842-ê°ì‚¬.jpg" into the database. The database is set to utf8_general_ci so I'm not sure what the problem could be. Any ideas?

Upvotes: 1

Views: 580

Answers (1)

Abhi Beckert
Abhi Beckert

Reputation: 33359

You need to make sure:

  1. the database/table/column is using UTF-8
  2. the database connection is using UTF-8
  3. the web browser is using UTF-8 when you upload the file
  4. the web browser is using UTF-8 when you display the file to the user later on
  5. that all your development tools (eg: phpMyAdmin) are using UTF-8 when you view the database

If there's any point, anywhere along the way, where something isn't using UTF-8 then things are going to go horribly wrong.

Note that web browsers usually do not use UTF-8 by default. So you will need to either include a <meta> tag instructing it to use utf-8, or manually set them to use it (in my browser, with View -> Text Encoding -> UTF-8).

Your php script is echoing the filename to the browser, but if it's just echoing the filename without a <meta> tag the browser will be trying to display it as some other encoding, perhaps latin1.

Upvotes: 3

Related Questions