Home Tutorial Php Examples fgets() example

 
 

fgets() example
Posted on: July 30, 2009 at 12:00 AM
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.

Related Tags for fgets() example:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.