title: Problems using PHP / fgetcsv when saving CSV data to MySQL
body: <p>Actually Iam reading a CSV file, I try using <code>fgetcsv</code> and opening as a socket with <code>fopen</code> + <code>fread</code>. The problem is when it save data to MySQL all data is shown but with jumps on the lines and spaces. </p>
  
  <p>I also made a MySQLi class to talk with the DB</p>
  
  <pre><code>            $c = 0;
              $arreglo = Array();
              if (($file = fopen("/pth/to/file/DataParaCortesProgramados.csv", "r")) !== FALSE) {
                  while (!feof($file)) {
                      $archivo = fread($file, 100000000);
                      $linea = explode("\n", $archivo);
                      $i = 0;
                      foreach ($linea as $campo) {
                          $camp = explode(",", $campo, 6);
                          if ($i &gt; 2) {
                              $arreglo[$c]["UN"] = $camp[0];
                              $arreglo[$c]["ncliente"] = $camp[1];
                              $arreglo[$c]["cliente"] = $camp[2];
                              $arreglo[$c]["suscrip"] = $camp[3];
                              $arreglo[$c]["tiposervicio"] = $camp[4];
                              $arreglo[$c]["datoservicio"] = $camp[5];
                              $c++;
                          }
                          $i++;
                      }
                  }
              }
              foreach ($arreglo as $row){
                  $ObjDBLink-&gt;INSERT("INSERT INTO `SuscripBI_copy` (`UN`,
                                                  `ncliente`,
                                                  `cliente`,
                                                  `suscrip`,
                                                  `tiposervicio`,
                                                  `datoservicio`) VALUES 
                                                  ('" . $row["UN"] . "', 
                                                   '" . $row["ncliente"] . "',  
                                                   '" . $row["cliente"]. "',
                                                   '" . $row["suscrip"]. "',
                                                   '" . $row["tiposervicio"] . "',
                                                   '" . $row["datoservicio"]. "')");
              }
  </code></pre>
  
  <p>==========================THIS IS WITH FGETCSV===========================</p>
  
  <pre><code>        if (($file = fopen("/path/to/file/" . trim($rowf), "r")) !== FALSE) {
              while (!feof($file)) {
                  $reg = fgetcsv($file, 1000, ",");
                  if ($rowc &gt; 2) {
                      $clientes[$i]["UN"]=$reg[0];
                      $clientes[$i]["ncliente"]=$reg[1];
                      $clientes[$i]["cliente"]=$reg[2];
                      $clientes[$i]["suscrip"]=$reg[3];
                      $clientes[$i]["tiposervicio"]=$reg[4];
                      $clientes[$i]["datoservicio"]=$reg[5];
          //            var_dump($clientes);
                  }
                  $i++;
                  $rowc++;
              }
          }
  
          foreach ($clientes as $cliente){
          //    var_dump($cliente);
              $ObjDBLink-&gt;INSERT("INSERT INTO `SuscripBI` (`UN`,
                                              `ncliente`,
                                              `cliente`,
                                              `suscrip`,
                                              `tiposervicio`,
                                              `datoservicio`) VALUES 
                                              ('" . trim($cliente["UN"]," \t\n\r\0\x0B") . "', 
                                               '" . trim($cliente["ncliente"]," \t\n\r\0\x0B") . "',  
                                               '" . trim($cliente["cliente"]," \t\n\r\0\x0B") . "',
                                               '" . trim($cliente["suscrip"]," \t\n\r\0\x0B") . "',
                                               '" . trim($cliente["tiposervicio"]," \t\n\r\0\x0B") . "',
                                               '" . trim($cliente["datoservicio"]," \t\n\r\0\x0B") . "')");
          }
  </code></pre>
  
  <p>This works, the data is saved on to the DB but it is saved with spaces or jumped lines
  suvch as this:</p>
  
  <blockquote>
    <p>|  E m p l e a d o s  |  1 1 3 7      |</p>
  </blockquote>
  
  <p>but need this:</p>
  
  <blockquote>
    <p>|  Empleados  |  1137      |</p>
  </blockquote>
  
  <p>I have tried <code>trim($row["UN"], " \t\n\r\0\x0B")</code>, or use <code>str_replace</code> before saving, but it always saves the same - incorrect - way.</p>
  
  <p>============SOLVED==============</p>
  
  <p>The problem was the encoding of CSV file, was UTF-16LE and PHP READ UTF-8, to solve this i change with a shell command from UTF-16LE to UTF-8.</p>
  
  <pre><code>iconv -f UTF-16LE -t UTF-8 $file  &gt; $file2
  </code></pre>