PHP file read

Home - Tutorials - Basics

Reading from file is a general task in PHP. This article tries to summarize how to use PHP file read possibilities.

Tutorial info:

Name:PHP file read
Total steps:1

Bookmark PHP file read

Step 1 - PHP file read methods

PHP file read

There are many different ways how you can read the content of a file.

Step 1.
First of all you need to open the file for reading using the fopen function. You need to provide the filename as parameter and you also need to define to open mode which is in case of reading the 'r'. Before any real reading begins you also need to check if file opening was success.

  1. $fileName = 'test.txt';
  2. $fileRes = fopen($fileName,'r');
  3. if ($fileRes === FALSE) {
  4.     echo 'Can not open file!';
  5. }

You can also check the file readability before trying to open it using the is_readable function as follows:

  1. $isReadable = is_readable($fileName);

Step 2.
So if the opening of the file was success then we can try to read some data from it. There are more solutions to do this:

Let's see them one by one. The most basic function to read a file is fread.

The syntax:

  1. string fread ( resource $handle , int $length )

Using fread you can read the complete file at once if you set the length parameter as big as the file size. You can use the filesize() function to do this and so you get the file content:

  1. $content = fread($fileRes, filesize($fileName));

You also can read the complete content by using the file_get_contents function like this:

  1. $content = file_get_contents($fileName);

Don’t forget that in this case you don’t need to open the file before reading. The file_get_contents do everything for you, you only need to provide the filename as parameter. Of course a readability check is recommended also in this way.

If the file is huge and/or you don’t want to read the complete content at once you can use fread to get the file content block by block in the following way:

  1. while (!feof($fileRes)) {
  2.   $block = fread($fileRes, 8192);
  3. }

You also can read the file content line by line using the fgets function. 

  1. while (!feof($fileRes)) {
  2.   $line = fgets($fileRes);
  3. }

It is also possible to read the complete file at once into an array using the file function. In this case every line of the file will be an element of the array. So if your file has 10 lines of text then you will get a string array with 10 elements. The file function similar to file_get_contents doesn’t require to use fopen. You need to provide the filename instead of file resource as parameter.

  1. $contentArray = file($fileName);

Step 3.
If you have used fopen to open a file you also need to close that file after you readed what you want. You can do this simple with fclose:

  1. fclose($fileRes);


As summary:
You need to use fopen, fclose and a file resource with fread and fgets.
You can simply use file and file_get_contents with a filename and no fopen, fclose is needed.

I hope this short lesson was usefull to see how to read file in PHP.


Tags: php file read, reading file with PHP, get file content, read file

PHP file read - Table of contents
Step 1 - PHP file read methods

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.1404