PHP 导入csv报错 xxx is an Invalid Spreadsheet file.
Published on in Posts
Short link: https://b.xyzt07.cn/s/9
由于表格内有特殊字符造成的,可以过滤掉控制符
$line = preg_replace('/[\x00-\x1F\x7F]/', '', $line);
整个文件重写
$file = fopen($filePath, 'r');
$filePath = tempnam(sys_get_temp_dir(), 'import_csv');
$fp = fopen($filePath, "w");
$n = 0;
while ($lines = fgetcsv($file)) {
$tmp = "";
foreach ($lines as &$line) {
$line = rtrim($line, "`");
$encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
if ($encoding != 'utf-8') {
$line = mb_convert_encoding($line, 'utf-8', $encoding);
}
$line = preg_replace('/[\x00-\x1F\x7F]/', '', $line);
}
$e = implode(',',$lines)."\n";
fwrite($fp, $e);
$n++;
}
fclose($file) || fclose($fp);