通过PHP导入CSV到mysql永远不会结束charset问题

时间:2016-03-21 09:03:49

标签: php mysql utf-8 character-encoding export-to-csv

在解决这个问题几天后,我需要请求帮助。

我制作了一个简单的scrpit,用于在CSV中上传PHP

到目前为止,它在我的Windows XAMPP上运行良好。

当我将脚本移动到我公司的Oracle Linux服务器时,我有很多字符集问题。像ö,ä,ü这样的德语变音符号显示为ö,Ã...

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.";
      }
   }
  } 

以下是编码为utf8的csv脚本。

    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位于utf8,默认字符集为utf8,我在utf8添加了内容类型的标头,我尝试将每个字符串转换为{{1编码仍然不起作用

你知道如何处理德国变音符号吗?

0 个答案:

没有答案