fgets() example


 

fgets() example

This is the example of fgets().

This is the example of fgets().

 fgets() function

fgets() function reads the data and returns a line from an open file. It begins to read from beginning and stop returning on a new line or on the specified length whichever comes first. By default it reads the first 1 kilo bytes, but it can be set lesser or larger. If this function is used in one file that has no line breaks, it returns False.  

Syntax

fgets(file,length)

Parameter Description

File is essential for running this function. The file must also be opened. 

Length is optional. Setting the length at specifies number of bytes will increase or decrease the file compilation. 

Example:

<?php
$file = fopen("upload.php", "r") or exit("Unable to open file!");

while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>

In this example, the output will either print the line or return False.

Ads