Signup/Sign In

Read a File in PHP

Reading content from a file in PHP can be accomplished in two ways:

  • Using readfile() function
  • Using fread() to read content from an open file.

Read File using readfile()

The readfile() function reads the entire content of a file and writes it to the output buffer.

If we have a file studytonight-info.txt with the following content:

Following are some of the subjects available on Studytonight:
C Language
C++
Data Structure
Android
Python

We can read the entire content of the above file and write it to the output buffer using the readfile() function.

<?php
echo readfile("studytonight-info.txt");
?>

Open and Read the File using fread()

We can read content from an already open file using the fread() function. The fread() function takes two arguments:

  1. first is the filename
  2. and the second argument specifies the size in bytes for the content to be read.

Let's take an example, if we have a file studytonight.txt with following content:

Studytonight is for coding enthusiasts!

If we want to read entire content of the file, we will open the file in read mode and then use the fread() function.

<?php
// Opening a file
$myfile = fopen("studytonight.txt", "r");
 
// reading the entire file using
// fread() function
echo fread($myfile, filesize("studytonight.txt"));
       
// closing the file
fclose($myfile);
?>

Studytonight is for coding enthusiasts!

As you can see in the above code snippet, we have used filesize() function to specify size in bytes equal to the entire file size, hence the fread() function reads entire content of the file.

But if you want to print only partial content from the file, you can directly specify the size in bytes(1 byte = 1 character) in the second argument like below,

<?php
// Opening a file
$myfile = fopen("studytonight.txt", "r");
 
// reading only 12 bytes using
// fread() function
echo fread($myfile, 12);
       
// closing the file
fclose($myfile);
?>

Studytonight


Read File Line by Line using fgets()

The fgets() function reads a single line(till the new line character) from any given file.

Remember the studytonight-info.txt file, lets use that file in the example below,

<?php
// Opening a file
$myfile = fopen("studytonight-info.txt", "r");
 
// reading a single line using fgets()
echo fgets($myfile);
       
// closing the file
fclose($myfile);
?>

Following are some of the subjects available on Studytonight:

After a call to the fgets() function the file pointer moves to the next line, so if we call the fgets() function twice, we will get two lines from the file,

<?php
// Opening a file
$myfile = fopen("studytonight-info.txt", "r");
 
// reading the first line using fgets()
echo fgets($myfile) . "<br>";
// reading the second line
echo fgets($myfile);
       
// closing the file
fclose($myfile);
?>

Following are some of the subjects available on Studytonight: C Language


Get End of File using feof()

The function feof() returns true if the file pointer is at the end of file, else it returns false.

This function can be used to loop through file of unknown size because feof() function can be used to check if the end-of-file is reached.

Let's use the feof() and fgets() function to read and echo/print the content of the file studytonight-info.txt line by line.

<?php
// Opening a file
$myfile = fopen("studytonight-info.txt", "r");
 
// loop around the file to output the content
// line by line
while(!feof($myfile)) {
    echo fgets($myfile) . "<br>";
}
       
// closing the file
fclose($myfile);
?>

Following are some of the subjects available on Studytonight: C Language C++ Data Structure Android Python


Read File Character by Character - fgetc()

We can use the function fgetc() to read single character from any file resource starting from the beginning.

We can use the fgetc() function along with feof() function to print the entire content of a file, character by character.

This function comes in handy if you have to replace any character or look for any particular character in file content.

Let's take an example,

<?php
// Opening a file
$myfile = fopen("studytonight-info.txt", "r");
 
// loop around the file to output the content
// character by character
while(!feof($myfile)) {
    echo fgetc($myfile);
}
       
// closing the file
fclose($myfile);
?>

Following are some of the subjects available on Studytonight:C LanguageC++Data StructureAndroidPython