fgets

(PHP 3, PHP 4 )

fgets -- Prende una riga da un puntatore a file

Descrizione

string fgets ( int fp, int length)

Restituisce una stringa di length - 1 byte letti dal file puntato da fp. La lettura termina quando sono stati letti length - 1 byte, in un caratere i newline (che viene incluso nel valore restituito), o alla fine del file (EOF) qualora giunga prima. Se no length is specified, the length defaults to 1k, or 1024 bytes.

Se si verifica un errore, restituisce FALSE.

Errori comuni:

Le persone abituate alla semantica 'C' di fgets notino la differenza nel trattamento dell'EOF.

Il puntatore al file deve essere valido e deve puntare ad un file correttamente aperto da fopen(), popen(), o fsockopen().

Segue un semplice esempio:

Esempio 1. Legge un file riga per riga

$fd = fopen ("/tmp/inputfile.txt", "r");
while (!feof ($fd)) {
    $buffer = fgets($fd, 4096);
    echo $buffer;
}
fclose ($fd);

Nota: The length parameter became optional in PHP 4.2.0

Vedere anche fread(), fopen(), popen(), fgetc(), fsockopen() e socket_set_timeout().