0

I have a script that takes a CSV file and uploads each row to a database. However, foreign characters in the file not just display wrong, but don't display at all.

For example, here is an "input" row, and what it would read like in the database:

"This café is amazing", "1000", "www.example.com"
"This caf", "1000", "www.example.com"

This is the script that does the upload:

header("Content-Type: text/plain; charset=utf-8");

if (!isset($_POST['form_id'])) {
    header("Location: ./?error=form_id");
    exit();
}
$form_id = $_POST['form_id'];

$csv = $_FILES['csv_file'];

if ($csv['size'] > 0) {
    $handle = fopen($csv['tmp_name'], "r");
    require_once("db.php");
    $i = (!isset($_POST['csv_headers']) ? 1 : 0);
    while ($data = fgetcsv($handle, 1000, ',', '"')) {
        if ($i > 0) {
            $data[2] = str_replace(" + ", "+", $data[2]);
            if (strpos($data[3], "http") === false && $data[3] != "") {
                $data[3] = "http://" . $data[3];
            }
            mysql_query("INSERT INTO exhibitor_lists SET form_id = $form_id, company_name = '" . addslashes($data[0]) . "', country = '" . addslashes($data[1]) . "', stand_number = '" . addslashes($data[2]) . "', web_address = '" . addslashes($data[3]) . "', logo_file = '" . addslashes($data[4]) . "', added_date = NOW()");
        }
        $i++;
    }
} else {
    header("Location: ./?error=csv_file");
    exit();
}

mysql_close();
header("Location: ./?success=upload");
exit();

Even when setting the content type header(), I get the problem.

mpdc
  • 3,550
  • 5
  • 25
  • 48
  • header isn't going to affect a file upload. it just tells the browser how to read the response. You need the file to be the correct encoding. Check out http://stackoverflow.com/questions/505562/detect-file-encoding-in-php – danielson317 Mar 17 '15 at 15:17
  • You need to learn what encodings are and how to handle them. You will also have to learn how to escape data properly. Start with [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/), [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) and [The Great Escapism (Or: What You Need To Know To Work With Text Within Text)](http://kunststube.net/escapism/). – deceze Mar 17 '15 at 15:55

1 Answers1

0

Save the CSV file in UTF8 before uploading it.

sleepless_in_seattle
  • 2,132
  • 4
  • 24
  • 35