这是我的Csv上传脚本如何让它忽略我的Csv文件的前两行。 找不到合适的方法来跳过/忽略它。 有点帮助吗?
<?php
include 'connect.php';
if ( isset( $_FILES['userfile'] ) )
{
$csv_file = $_FILES['userfile']['tmp_name'];
if ( ! is_file( $csv_file ) )
exit('File not found.');
if (($handle = fopen( $csv_file, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
{
$sql = "INSERT INTO `prelims` (`name`,`grade`) VALUES('$data[0]','$data[2]')";
$exec = mysql_query($sql) or die(mysql_error());
echo ("The following data has been added to the database");
}
}
}
}
?>
答案 0 :(得分:1)
fgetcsv($handle); // get line 0 and move pointer to line 1
fgetcsv($handle); // get line 1 and move pointer to line 2
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
...
您可能还希望在此基础上进行一些错误检查,以确保您的CSV中至少包含2行。
答案 1 :(得分:0)
请注意,上述部分答案不正确。这条线
fseek($handle, 2);
不向前寻找两条线;它寻求前进两个字节。要跳过任意数量的行,您可以这样做:
$skipLines = 5000; // or however many lines you want to skip
$lineNum = 1;
if ($skipLines > 0) {
while (fgetcsv($handle)) {
if ($lineNum==$skipLines) { break; }
$lineNum++;
}
}
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// starts getting data from 5001st row
}