Thursday 25 April 2013

Reading Data From File in File system

Often we need to read the data from the file that is present on our file system.
PHP provides a very good support for file handling .

In this post I will mainly be concerned about reading of data from file .
Writing and updating new content and deleting file will be explained in upcoming post.

For reading file we have many inbuilt functions at our disposal.
In this article we will go through 
a) fread() and required fopen() and fclose()
b) file_get_contents.
c) file()

METHOD ONE : Reading file using fread()

First let we have a sample text file 'sample.txt' in our file system.
For testing let its contents be 
    
This is sample test file.
It contains the dummy contents.


Now we have to read its contents using php.

step 1 : Open the file using fopen() in read mode.
step 2 : read the contents using fread()
step 3 : close the file handler.

When we read the file using fopen() it gives us the pointer or you can say a resource handler to handle the file.
Its takes the file path and mode as mandatory parameters

<?php
fopen("path to sample.txt", "r");
?>

A file may be opened in following modes other then read mode
MODEDESCRIPTION
r Read only.
r+ Read/Write.
w Write only. over write the contents of file or creates a new file if it doesn't exist
w+ Read/Write. over write the contents of file or creates a new file if it doesn't exist
a Append. Add the contents to the end of file or creates a new file if it doesn't exist
a+ Read/Append. Add the contents to the end of file
x Write only. Creates a new file.
x+ Read/Write. Creates a new file.

 If you want to read more about it go here

once we have appropriate handle we can read it using fread()

It has 2 parameters 
 ==> file handler
 ==> Length in bytes you want to read 


$contents = fread($handle,1025);

Now we have all contents of text file in $contents .We will now close the file and echo its contents.

 fclose($handle);
 echo $contents;

So complete program will be like this
<?php
 $handle   = fopen("sample.txt", "r");
 $contents = fread($handle,1000);
 fclose($handle);
 echo $contents;
?>




This function returns false on failure.
You can read official documentation for more info.


METHOD TWO : Reading file using file_get_contents();

This is yet another function for reading the file ..

This difference beween this and fread() is that you need not to explicitly open and close the file .

<?php

$contents =file_get_contents('path to file');

echo $contents;

?>



Read more here


METHOD THREE

The last method that I am going to discuss here is file().
This method will read the file and convert it into array.In this method also you need not to open the file manually.

<?php
 $lines = file('path to sample.txt',FILE_IGNORE_NEW_LINES);
 foreach($lines as $line)
 { 
   echo $line;
 }
?>
I have used FILE_IGNORE_NEW_LINES to ignore '\n' that would have else been printed at end of each line.

The other available options are .
FILE_USE_INCLUDE_PATH  : search for file in include path
FILE_SKIP_EMPTY_LINES    : skip empty lines


You can read in more detail  here.


Quite simple .. isn't it..?