通过csv文件的前两行

时间:2017-11-20 09:22:26

标签: php fgets

我正在尝试使用fgets从csv文件中读取数据。但是,我希望通过前两个顶行。

默认情况下,fgets仅传递第一行。如何通过前两个顶行来调整它?

$myfile = "/home/dibon/AML_Data/test.csv";
$myfile = fopen("$myfile", "r") or die("Unable to open file!");  

fgets($myfile); //First fgets to read over header line.

while($line=fgets($myfile)){
//Explode your line by space delimeter
$words=explode("|",$line);

DB::table('transaction_incremental')->insert(
['ReceiptNumber' => "$words[0]", 'InitiatingMSISDN' => "$words[2]"]
);

}

1 个答案:

答案 0 :(得分:0)

fgetcsv($handle); // get line 0 and move to next
fgetcsv($handle); // get line 1 and move to next
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    ....
}