如何在php中逐行阅读

when i try to insert each line into my oracle database i get an error stating invalid number, but if have only one line in the file it works fine.

$file = @fopen('file.text', "r") ;  


// while there is another line to read in the file
while (!feof($file))
{
    // Get the current line that the file is reading
    $currentLine = fgets($file) ;
    $currentLine = explode('        ',$currentLine) ;
    insert($currentLine) ;


}   

fclose($file) ;

the lines look like this

1     4     100
1     4     101

Your explode() call is using 9 spaces as a separator, but your file only appears to have 5 spaces between each number. This means your $currentline be a single element array containing the original line, and not separate elements with a number in each.

Either change the number of spaces in the explode call, or change to

$currentLine = preg_split('/\s+/', $currentLine);

which will split on any number of sequential spaces.

Try this:

$currentLine = trim(fgets($file)); 

It's possibly failing on the newline/carriage-return at the end of the line.

If not, where is this insert() function defined? Build a debug section that echos or writes out the attempted queries so you can really see the issue.

$lines = explode("
", str_replace("", "", file_get_contents("file.text")));

foreach ($lines as $line)
{
     insert(explode('        ', $line);
}

I would double check that there is not a new line at the end of the file. Maybe even double check inside of php that the line read is not blank.

$lineCount=0;
// while there is another line to read in the file
while (!feof($file))
{
    $lineCount++;
    // Get the current line that the file is reading
    $currentLine = fgets($file);
    if(trim($currentLine) != ''){
        $currentLine = explode('        ',$currentLine) ;
        insert($currentLine) ;
        echo "Inserted line number $lineCount<br />";
    } else {
        echo "There was a blank line at line number $lineCount.<br />";
    }
} 

try this :

// Read all data in the file
$file_all = file_get_contents('file.text') or die("unable to open file");
// create an array with each index = a line
$file_lines = explode(chr(13),$file_all);
// Insert each line found
foreach ($file_lines as $file_line) { insert($file_line); };