Monday 24 November 2014

PHP file read

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.
Code:
  1. $fileName = 'test.txt';
  2. $fileRes = fopen($fileName,'r');
  3. if ($fileRes === FALSE) {
  4.     echo 'Can not open file!';
  5. }
  6.  
You can also check the file readability before trying to open it using theis_readable function as follows:
Code:
  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:
  • Read the file at once into a string at once
  • Read the file at once into an array
  • Read the file line by line
  • Read the file block by block
Let's see them one by one. The most basic function to read a file is fread.

The syntax:
Code:
  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:
Code:
  1. $content = fread($fileRes, filesize($fileName));
You also can read the complete content by using the file_get_contents function like this:
Code:
  1. $content = file_get_contents($fileName);
Don’t forget that in this case you don’t need to open the file before reading. Thefile_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:
Code:
  1. while (!feof($fileRes)) {
  2.   $block = fread($fileRes, 8192);
  3. }
You also can read the file content line by line using the fgets function. 
Code:
  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 filefunction. 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. Thefile function similar to file_get_contents doesn’t require to use fopen. You need to provide the filename instead of file resource as parameter.
Code:
  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:
Code:
  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.

0 comments:

Post a Comment