实际上,我是在读取CSV文件,我尝试使用fgetcsv
并使用fopen
+ fread
作为套接字打开。问题是,当它将数据保存到MySQL时,将显示所有数据,但在行和空格处会跳转。
我还制作了一个MySQLi类来与数据库对话
$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 > 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->INSERT("INSERT INTO `SuscripBI_copy` (`UN`,
`ncliente`,
`cliente`,
`suscrip`,
`tiposervicio`,
`datoservicio`) VALUES
('" . $row["UN"] . "',
'" . $row["ncliente"] . "',
'" . $row["cliente"]. "',
'" . $row["suscrip"]. "',
'" . $row["tiposervicio"] . "',
'" . $row["datoservicio"]. "')");
}
========================这就是FGETCSV ================= ==========
if (($file = fopen("/path/to/file/" . trim($rowf), "r")) !== FALSE) {
while (!feof($file)) {
$reg = fgetcsv($file, 1000, ",");
if ($rowc > 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->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") . "')");
}
这有效,数据被保存到DB上,但是用空格或跳线保存 像这样:
|电子邮件| 1 1 3 7 |
但是需要这个:
| Empleados | 1137 |
我尝试过trim($row["UN"], " \t\n\r\0\x0B")
,或者在保存之前使用了str_replace
,但是它总是以相同的方式保存-错误的方式。
============已解决==============
问题是CSV文件的编码是UTF-16LE和PHP READ UTF-8,为解决此问题,我使用了从UTF-16LE到UTF-8的shell命令进行了更改。
iconv -f UTF-16LE -t UTF-8 $file > $file2