PHP.nl

fgets

fgets

Gets line from file pointer

 **fgets** resource $stream  $length

Gets a line from file pointer.

stream``length Reading ends when - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. length

Returns a string of up to - 1 bytes read from the file pointed to by . If there is no more data to read in the file pointer, then false is returned. length``stream

If an error occurs, false is returned.

Voorbeeld: Reading a file line by line

<?php

$fp = @fopen("/tmp/inputfile.txt", "r");

if ($fp) {
    while (($buffer = fgets($fp, 4096)) !== false) {
        echo $buffer, PHP_EOL;
    }

    if (!feof($fp)) {
        echo "Error: unexpected fgets() fail\n";
    }

    fclose($fp);
}

?>

Opmerking: > People used to the 'C' semantics of should note the difference in how is returned. fgets``EOF

fgetss``fread``fgetc``stream_get_line``fopen``popen``fsockopen``stream_set_timeout