Read a file line by line php

How to read a file [Line by Line] and [Character by Character] in php?

Answer:
The fgets() function is used to read a single line from a file.

Example:
$file = fopen("abc.txt", "r") or exit("Error while opening file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "
";
}
fclose($file);
?>

The fgetc() function is used to read a single character from a file

Example:
$file=fopen("abc.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>

0 comments:

Post a Comment