1

After working on this Problem for several Days now i need to ask for help me.

I've made a simple scrpit for uploading CSV in PHP.

It's working fine on my Windows XAMPP so far.

While i moved the script to a Oracle linux server in my company i have a lot of charset issues. German umlauts like ö, ä, ü are displayed like ö, Ã...

function uploading($tmp,$name,$size,$type){
    $target_dir = "import/";
    $target_file = $target_dir . basename("".date('m-d-Y_H_i_s').".csv");
    $uploadOk = 1;
    // Check if image file is a actual CSV or fake 
    if(isset($_POST["submit"])) {
        $mimes = array('application/vnd.ms-excel','text/plain','text    /csv','text/tsv');
        if(in_array($_FILES['fileToUpload']['type'],$mimes)){
           $uploadOk = 1;
        } else {
          die("Sorry, mime type not allowed");
          $uploadOk = 0;
        }
     } 

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],  $target_file)) {


        import($target_file);

    } else {
        echo "Sorry, there was an error uploading your file.";
      }
   }
  } 

Here is the csv script with encoding to utf8.

    header("Content-Type: text/html; charset=utf-8");
    if (($handle = fopen($csv_file, "r")) !== FALSE) {
       fgetcsv($handle);   
       while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
            $num = count($data);
            for ($c=0; $c < $num; $c++) {
              $col[$c] = $data[$c];

            }
        //direct encode for testing
        $col1 = utf8_encode($col[2]);
        $col2 = utf8_encode($col[3]);
        $col3 = utf8_encode($col[4]);
        $col4=  utf8_encode($col[1]);
        $col5=  utf8_encode($col[5]);
        echo $col2;
        echo $col1;

Th CSV is in utf8, default charset is utf8, i added a header for content type in utf8 and i try to convert every string to utf8 and the encoding still doesn't work

Do you have any idea how to handle the German umlauts?

MT0
  • 143,790
  • 11
  • 59
  • 117
Evolo
  • 13
  • 4
  • You claim that CSV uses UTF-8, yet you use `utf8_encode()` to convert from ISO-8859-1. Do you really know what encoding your data is using? – Álvaro González Mar 21 '16 at 10:17
  • No, not really. Do you know how i can find it out? – Evolo Mar 21 '16 at 11:22
  • Well... You can load the file in a text editor that supports different encodings and then try the most usual ones until the text looks correct. – Álvaro González Mar 21 '16 at 11:25
  • OK now i know what you men. I've open the file with npp, it shows correctly with utf8, and the way its displayed in Ansi was the same like in the Browser output. Now i removed the utf8_encode function and now the browser output is correct, but the import to Mysql database is wrong anyway – Evolo Mar 21 '16 at 11:36
  • Please let me close this question as dupe. I think you're failing to declare encoding when you connect to MySQL and the linked question contains information for several libraries. If it doesn't apply tell me so and I'll retract the vote. – Álvaro González Mar 21 '16 at 12:50
  • Search this forum for "Mojibake"; I've answered it serveral times. – Rick James Mar 23 '16 at 20:37
  • Do not use `utf8_encode` or any other conversion function. – Rick James Mar 23 '16 at 20:37
  • @RickJamesYou made it :D ive simply deleted all encoding and charset parameters in ini and html files. Now it works. Thanks for that :) – Evolo Mar 24 '16 at 09:06

0 Answers0