PHP Tutorial – File Handling

In this PHP web-programming language we will take a look at file handling. We will take a look at how to open en close a file, how to read a file line by line and how to read a file character by character.

File Opening Modes

Before we look how to open a file in PHP you need to know that a file can be opened in different modes. For example you can open a file in read only mode or in read and write modes. Take a look at the table below for the different modes:

Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists

Opening a File

To open a file PHP you can use the fopen() function. This function takes two parameters, where the first parameter contains the name of the file and the second parameter is the modes that should be used to open the file.

Take a look at an example:


<html>
<body>

<?php
$file=fopen("test.txt","w");
? >

</body>
</html> 

In this example we are trying to open een file test.txt in write modes. If the fopen() function is unable to open the specified file, it returns 0 (of false).

Closing a File

After you have opened a file and you are done (for instance reading its contents) then you should close the file. A fopen() function should always match with a fclose() function. Take a look at an example:


<?php
$file = fopen("test.txt","r");

//do something with the file test.txt

fclose($file);
?>

Reading a File Line by Line

The fgets() function is used to read a single line from a file. After the line has been read the file pointer is pointing to the next line in the file. This is very useful, because we could now read the file line by line.

Before we show you the example we have to talk about the feof() function. This function can be used to check if the “End-Of-File” (EOF) has been reached. This is very useful because we now can loop through a file of unknown length. We can do this on every file that is not opened in w, a, and x mode!

Let’s take a look at an example:


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

First we open the file test.txt in read mode. If this is not possible, we exit with a message. Than we use a while loop to loop through the file. This is done until we reach the end of the file. In the loop the fgets() function is used to grab one line and echo this line onto the screen. The last thing that is done is closing the file.

Reading a File Character by Character

The function fgetc() can be used to read a character from a file. We now can use the previous example to read a file character by character. Take a look at the following example:


<?php
$file = fopen("test.txt", "r") or exit("Unable to open the file!");
while(!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>

As you can see it is almost the same as the previous example. The only thing that’s changed is the function fgets() in fgetc() and the html break is removed.

That is all for this tutorial.

This entry was posted in PHP Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 6 responses to “PHP Tutorial – File Handling”

Why not let us know what you think by adding your own comment!

  1. as on May 1st, 2012:

    ok

  2. Foroz farhat on February 25th, 2013:

    Thanx a lot!

  3. kalyan on April 19th, 2013:

    nice….

  4. kalyan on April 19th, 2013:

    BUT it is better if u add advanced php concepts in this site…

  5. Dinusha on September 2nd, 2013:

    Hi anyone can please tell me how to insert and read data in text file table? I want to insert data for table in text file and read the tables. I does not required any data base. Please help me

  6. Md. Arif Hossain on April 14th, 2014:

    Thanks for guideline.