• Tutorial List

PHP Read & Close files

Last updated Jul 08, 2020

How to read files in PHP?

In PHP, there are several ways to read files. In the following, we will look at all the possible ways to read files.


The my.txt File

That is the file which we will try to read. Inside of the my.txt file is –

Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.

The PHP readfile() function

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

The index.php and my.txt file are in the same folder, that’s why I just entered only the filename. Otherwise, I would have to enter the full path of the my.txt file.

<pre>
	<?php
  		echo readfile('my.txt');
  	?>
</pre>
Browser Output
Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.107

Reading File using the feof(), fgets() function & while loop

PHP feof() function

  • The PHP feof() function used to check the end of an opened file.
  • At the end of the file, it will return true, otherwise, it will return false.

PHP fgets() function

  • The PHP fgets() function is used to read a single line of an opened file.
<?php
  $myFile = fopen('my.txt','r') or die("Oops, There is an error.");
  echo fgets($myFile);
?>
Browser Output
Lorem ipsum dolor sit amet.

Reading the whole file

Reading the whole file with the help of the while-loop and the above two functions.

<pre>
    <?php
        $myFile = fopen('my.txt','r') or die("Oops, There is an error.");
        while(!feof($myFile)){
            echo fgets($myFile);
        }
    ?>
</pre>
Browser Output
Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.

How to read a single character from a file using PHP?

  • fgetc() function
    • It will return the first character from the file pointer.
<?php
    $myFile = fopen('my.txt','r') or die("Oops, There is an error.");
    echo fgetc($myFile);
?>
Browser Output
L

How to read a whole file character by character?

By the help of the fgetc() function & the while-loop, you can read a whole file character by character.

<?php
    $myFile = fopen('my.txt','r') or die("Oops, There is an error.");

    while($char = fgetc($myFile)){
        if($char == "L"){
            echo '<br><strong>'.$char.'</strong><br>';
        }
        else{
            echo $char;
        }
    }
?>
Browser Output

L
orem ipsum dolor sit amet.
L
orem, ipsum dolor.
L
orem ipsum dolor, sit amet consectetur adipisicing elit.

How to read a file in an array?

  • file() function
    • It will return a numeric array, you can read each line by the array index number.
    • You don’t have to open that file you want to read.
<?php
    $myFile = file('my.txt');
    echo $myFile[0];
?>
Browser Output
Lorem ipsum dolor sit amet.

Reading the whole file using the foreach loop & the file() function –

<pre><?php
        $myFile = file('my.txt');
        
        foreach($myFile as $singleLine){
            echo $singleLine;
        }
    ?>
</pre>
Browser Output
Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.

How to read a file by binary length or chunks?

The fread() function reads up to length bytes from the file pointer referenced by the file-handle.

This function stops reading when one of the following conditions is found –

  • length bytes have been read
  • End of the file is reached
  • A packet becomes available or the socket timeout occurs (for network streams)
  • If the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.

Syntax of the fread() function

<?php
 	fread(FILE_HANDLE, SIZE);
?>
  • FILE_HANDLE
    • the file handle is an opened file.
  • SIZE
    • How many sizes (bytes) you want to read.

Example –

In the following example, I read only 10 bytes of data from the my.txt file.

<?php
    $myFile = fopen('my.txt','rb') or die("Oops, There is an error.");
    echo fread($myFile, 10);
?>
Browser Output
Lorem ipsu

Reading the whole file using the fread() function

To read the entire file using the fread() function, you must have the full size of the file.

  • PHP filesize() function
    • The filesize() function returns the full size of a file.
    • It returns the size in bytes.
<?php
    $fileSize = filesize('my.txt');
    echo $fileSize;
?>
Browser Output
107

Reading the whole file

<pre><?php
  
    $myFile = fopen('my.txt','rb') or die("Oops, There is an error.");
    $fileSize = filesize('my.txt');

    echo fread($myFile, $fileSize);

?></pre>
Browser Output
Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.

How to Close a File in PHP?

In all the above examples we have not closed the files after opening the file. But, after opening and manipulating files you must have to close the opening file.

To close a file, you have to use the fclose() function. This function only takes the file-handle.

After closing the file, you can’t access the file-handle.

<pre><?php
  $myFile = fopen('my.txt','r') or die("Oops, There is an error.");

  while(!feof($myFile)){
  	echo fgets($myFile);
  }

  fclose($myFile);

  /*
  you can't do this again 
  after closing the file

  while(!feof($myFile)){
      echo fgets($myFile);
  }
  */
?></pre>
Browser Output
Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.

How to read a file without opening & closing the file?

To achieve that we will use one more PHP built-in function, and that is file_get_contents() function.

<pre><?php

    $myFile = file_get_contents('my.txt');
    echo $myFile;

?></pre>
Browser Output
Lorem ipsum dolor sit amet.
Lorem, ipsum dolor.
Lorem ipsum dolor, sit amet consectetur adipisicing elit.

Ask Questions

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