PHP File Read

The PHP File Read and close methods will be covered in this chapter.

PHP Read File – fread()

The fread() method reads data from a file that is currently open. The first parameter of fread() indicates the filename to read from, while the second defines the maximum number of bytes to read.

The fread() function terminates when it reaches the end of the file or the length given as a parameter, whichever comes first. The file and the length of the file to be read are sent to the fread() method, which returns the read string if it succeeds or FALSE if it fails.

Syntax:

string fread ( $file, $length )

Parameters of fread():

In PHP, the fread() method takes two parameters.

ParameterDescription
$fileThis is a required argument that specifies the file
$lengthIt’s a required parameter that determines the maximum number of bytes that must be read.

PHP Close File – fclose()

To close an open file, fclose() method is used.

Note: Closing all files after you’ve finished with them is good programming practice. You don’t want an open file consuming system resources on your server.

Syntax:

bool fclose( $file )

Parameter of fclose():

The $file parameter is the only one accepted by PHP’s fclose() function. The file that has to be closed is specified by this argument.

Let’s take a look at the file we want to open, and then we will see an example of how to open it.

The text file “languages.txt” will be used.

HTML HyperText Markup Language 
CSS  Cascading Style Sheet 
PHP PHP Hypertext Preprocessor 
SQL  Structured Query Language
<h2>Examples for Reading specific bytes</h2>

<?php
$file = fopen("languages.txt", "r") or die("Unable to open file!");
echo fread($file,"21");// reading 21 bytes from the file
fclose($file);
?>
PHP File Read
<h2>Examples for File Reading </h2>

<?php
$file = fopen("languages.txt", "r") or die("Unable to open file!");
echo fread($file,filesize("languages.txt"));
fclose($file);
?>
File Read

PHP Read Single Line – fgets()

To read a single line from a file, fgets() function is used.

The following example displays the first line of the languages.txt” file:

<h2>Read Single Line Example</h2>

<?php
$file = fopen("languages.txt", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);

?>

Note: The file pointer has moved to the next line after calling the fgets() function.

PHP Check End-Of-File – feof()

The feof() function determines whether or not the “end-of-file” (EOF) has been reached. For looping through data of unknown length, the feof() function is useful.

The following example reads the “languages.txt” file line by line until it reaches the end:

<h2>Check End of file Example</h2>

<?php
$file = fopen("languages.txt", "r") or die("Unable to open file!");
while(!feof($file)) {
  echo fgets($file) . "<br>";
}

fclose($file);

?>

PHP Read Single Character – fgetc()

To read a single character from a file, use the fgetc() function.

The following example reads the “languages.txt” file character by character until it reaches the end:

<h2>Read Single Char Example</h2>

<?php
$file = fopen("languages.txt", "r") or die("Unable to open file!");
while(!feof($file)) {
  echo fgetc($file);
}

fclose($file);

?>

Note: The file pointer has moved to the next character after calling the fgetc() function.