我们正在尝试通过 PHP 将 CSV 文件导入 MySQL。字符编码为 utf-8(经过检查、规范化和验证)。使用 MySQL 客户端界面工具导入时,所有行都存在。代码如下:
$handle = fopen($this->file_name, 'r+') or die ('cant open a '.$this->file_name);
$new_filename = $upload_directory.'/'.date('Y_m_d_H_i_s').'_data_import.csv';
$for_import = fopen($new_filename, 'x+') or die ('cant open a '.$new_filename.'_for_import');
$data = array();
$row_counter = 0;
while (($row = fgetss($handle)) !== false)
{
$row = fgetss($handle);
if (($row_counter > $this->rows_to_ignore))
{
$enc = mb_detect_encoding($row);
$row = iconv($enc, 'UTF-8', $row);
$row = str_replace(", ", '\\, ', $row);
$row = trim($row);
$row = str_getcsv($row, ',', '"', '\\');
fputcsv($for_import, $row, ',', '"');
}
$row_counter++;
}
fclose($for_import);
fclose($handle);
row_counter 变量返回所有行的大约 50%。有任何想法吗?
谢谢