PHP fgets, fgetc and feof functions



PHP fgets(), fgetc() and feof() file handling

In this php tutorial we will learn how use fgets() function, fgetc() function and feof() function with php example.

PHP File handling functions :

  • fgets() – used to read a single line from a file.
  • fgetc() – used to read a single character from a file.
  • feof() – used to check ‘end of file’.

PHP fgets() Function

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

fgets() function syntax

<?php

fgets(“filename”);

?>

PHP fgets() function example

The fgets() function read a single first line from a file. For read a file we must first open file using fopen() function and the read a line using fgets() function after using fgets() function we have to close the opened file using fclose() function. Here, we open a ‘abc.txt’ file in read mode(‘r’) and read a line using fgets() function in below php example.

<?php

$file=fopen("abc.txt","r");

$readfile=fgets($file);
echo $readfile;

fclose($file);

?>

The out put is : Read only first single line from a file. “Hello Meera Academy”. Now, lets take a php form example for fgets() function. First we create a new file with some text contents and after we read file using fgets() function the output will be the first line from a file.

PHP fgets() function example

<html>
<head>
<title>PHP fgets() Example</title>
</head> 
<body>
<FORM method="POST">
Enter String1 : <input type="text" name="str"> <br/> 
Enter String2 : <input type="text" name="str1"> <br/> <br/>
<input type="submit" name="Submit1" value="Create File">
<input type="submit" name="Submit2" value="Read Fiel - gets()">
</FORM>
<?php
if(isset($_POST['Submit1']))
{
//open file abc.txt in append mode
$myfile = fopen("abc.txt", "a");

$text = $_POST["str"]. PHP_EOL . $_POST["str1"];
fwrite($myfile, $text);

fclose($myfile); 
echo "file created !!";
}
if(isset($_POST['Submit2']))
{
$file=fopen("abc.txt","r");

$readfile=fgets($file);
echo $readfile;

fclose($file);
}
?>

The out put is :

PHP fgets function example
PHP fgets() function example

 

PHP fgetc() Function

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

fgetc() function syntax

<?php

fgetc(“filename”);

?>

fgetc() function example

The fgetc() function rad a single first character from a file. Here, we open a ‘abc.txt’ file in read mode(‘r’) and read a character using fgetc() function in below php example.

<?php

$file=fopen("abc.txt","a");
fwrite($myfile, "Meera Academy");

$readfile=fgetc($file);
echo $readfile;

fclose($file);

?>

The out put is : “M”. Read only first character from a file.

PHP form example for fgetc() function.

<html>
<head>
<title>PHP fgetc() Example</title>
</head> 
<body>
<FORM method="POST">
Enter String : <input type="text" name="str"> <br/> <br/>

<input type="submit" name="Submit1" value="Create File">
<input type="submit" name="Submit2" value="Read File - getc()">
</FORM>
<?php
if(isset($_POST['Submit1']))
{
//open file abc.txt in append mode
$myfile = fopen("abc.txt", "a");

$text = $_POST["str"];
fwrite($myfile, $text);

fclose($myfile); 
echo "file created !!";
}
if(isset($_POST['Submit2']))
{
$file=fopen("abc.txt","r");

$readfile=fgetc($file);
echo $readfile;

fclose($file);
}
?>

The output is:

PHP fgetc function example
PHP fgetc() function example

PHP feof() Function

The feof() function is used to check whether the file has been reached to end or not. This function used in looping for get all line from file one by one to check the end of file reached or not.

feof() function syntax

<?php

feof(“filename”);

?>

php feof() example :

<?php

$file = fopen("abc.txt","r");

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

fclose($file);

?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *