Search code examples
phpmysqltext-filesfopenfgets

How to exclude the first line from a text file using php


My text file sample.txt. I want to exclude the first row from the text file and store the other rows into mysql database.

ID Name EMail

1  Siva [email protected]

2 vinoth [email protected]

3 ashwin [email protected]

Now I want to read this data from the text file except the first row(ID,name,email) and store into the MYsql db.Because already I have created a filed in database with the same name.

I have tried
$handle = @fopen($filename, "r"); //read line one by one

while (!feof($handle)) // Loop till end of file.
{
    $buffer = fgets($handle, 4096); // Read a line.
}
print_r($buffer); // It shows all the text.

Please let me know how to do this?

Thanks. Regards, Siva R


Solution

  • You can just call fgets once before your while loop to get the header row out of the way.

    $firstline = fgets($handle, 4096);
    while (!feof($handle)) // Loop till end of file.
    { ...