Working with files in PHP


File Handling in PHP

Hi guys, In this article we are going to understand the handling of files with a practical approach. File handling functions in PHP are extremely useful and user-friendly. PHP file handling functions are used in different conditions in any script development. You need to install XAMPP server. These built in functions give flexibility in developing complex scripts.

Operations on a file

  • Opening the file
  • Closing the file
  • Reading a file line by line
  • Checking the end of file
  • Reading a file character by a character

Opening the file in PHP

For opening a file we use the fopen() function. We can open a file or a URL to read by using the fopen() function of PHP. While opening we can give the mode of file open ( read, write.. etc ). By using fopen we can read any external URL also.

PHP scripting for the Open file

<html>
<
body>
<?php
$file=fopen("yourfile.txt","r");
?>
</body>
</html>

Save it as fileopen.php

To run, open the browser and type in the URL http://localhost/yourfolder/fileopen.php.

Now here I am going to tell you that,the file can be opened in different mode, you can use any of the mode as per your requirement.

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.

Closing the file in PHP

From the above description we learn how to open a file now we will learn to close a open file so for this purpose we use the inbuilt function fclose() .
You should close all files after you have finished with them because it's a good programming practice. Discuss the importance of closing a file.

PHP scripting for closing the file

<html
>
<
body>
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
</body>
</html>

Save it as fileclose.php

To run open browser and type in the URL http://localhost/yourfolder/fileclose.php.

Reading a File Line by Line in PHP

The fgets() function is used to read a single line from a file.

PHP scripting for reading a file line by line

<html>
<
body>
<?php
$doc = fopen("yourfilename.txt", "r") or exit("Unable to open file!");
//result : A line of the file until the end is reached
while(!feof($doc))
  {
 
echo fgets($doc). "<br />";
  }
fclose($doc);
?>
</body>
</html>

Saved it by rdline.php

To run open browser and type in the URL http://localhost/yourfolder/rdline.php.


Check End-of-file in PHP

There are two function which can be used to find the end of file. T
he feof() function checks if the "end-of-file" (EOF) has been reached. The feof() function is useful for looping through data of unknown length. You cannot read from files opened in w, a, and x mode. The scripting for understand with practical approach are as follow :

if (feof($file)) echo "End of file";

Reading a file character by a character in PHP

The fgetc() function is used to read a single character from a file. This is to reads a file character by character, until the end of file is reached:

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

Saved it as readchar.php

To run, open the browser and type in the URL http://localhost/yourfolder/readchar.php.


Similar Articles