通常可以使用
fgetcsv函数读取CSV文件(根据您的CSV文件类型,您可能必须指定分隔符,分隔符,…作为参数)
这意味着逐行浏览文件并不比这样的事情困难得多:
$f = fopen('/path/to/file', 'r');
if ($f) {
while ($line = fgetcsv($f)) { // You might need to specify more parameters
// deal with $line.
// $line[0] is the first column of the file
// $line[1] is the second column
// ...
}
fclose($f);
} else {
// error
}
当然,您必须获得上传文件的正确路径 – 请参阅$_FILE超全局和Handling file uploads部分,了解有关该文件的更多信息.
并且,要将数据保存到数据库中,您必须使用适合您的数据库引擎的API – 如果使用MySQL,您应该使用:
>请注意,您应该更喜欢mysqli,而不是旧的mysql扩展(不支持MySQL中添加的功能> = 4.1)