N00bMagg
N00bMagg

Reputation: 11

fgets skip lines, it doesn't read all lines

I'm new to fgets, I have this code to read the row, but apparently until it reaches that line it will skip for no reason... Here is the data in my txt file that I make it to read.

ABCDEF1   SDDFS775A                                        QQ            O9   
ABCDEF1   SDDFS77577432B                                                            3.00                                                                                                        
ABCDEF1   1234567C     newCODE    123456       123456789                        83131          DIED
ABCDEF1   1234567C                                               999999                                9999999  999999                  999999
ABCDEF1   1234567D            20170606                51QAZZ  345DDW                                                  LOVE   

Code:

$handle = @fopen($name, 'r');
if ($handle) {
    while ($row = fgets($handle,4096)) {
        echo $row;
    }
}

I can read until this row

ABCDEF1   1234567C                                               999999 

but I can't read this row

ABCDEF1   1234567D            20170606                51QAZZ  345DDW                                                  LOVE   

Any reason why?

Upvotes: 1

Views: 885

Answers (1)

xsami
xsami

Reputation: 1330

The problem with your code is with the method fgets: string fgets ( resource $handle [, int $length ] ). The second parameter is the length of the characters that will read. See the documentation here

You should leave the second parameter empty. Try this:

$fp = fopen("fichero.txt", "r");
   while(!feof($fp)) {
       $linea = fgets($fp);
       echo $linea . "<br />";
}
fclose($fp);

Upvotes: 2

Related Questions