FILE HANDLING IN PHP

In PHP we will take a look at file handling. We will take a look at how to open , close a file, how to read a file line by line and how to read a file character by character.

ModesDescription
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
wWrite 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
aAppend. 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
xWrite 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

Open a File with PHP

To open a file, the fopen() function is used. There are many reasons behind opening a file, like reading content of the file, writing new content to a file or appending additional content to the already existing content in the file.

ex:-

$myfile = 'file.txt';
//opens the file.txt file
$handle = fopen($myfile, 'w') or die('Cannot open file: '.$myfile);

Closing a File

After you have opened a file and you are done then you should close the file. A fopen() function should always match with a fclose() function.

ex:-

$myfile = ‘file.txt’;
//opens the file.txt file or implicitly creates the file
$handle = fopen($myfile, ‘w’) or die(‘Cannot open file: ‘.$myfile);

// closing a file
fclose($handle);

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” 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 model.

ex:-

<?php
// Opening a file
$myfile = fopen("studytonight-info.txt", "r");
 
// loop around the file to output the content
// line by line
while(!feof($myfile)) {
    echo fgets($myfile) . "<br>";
}
       
// closing the file
fclose($myfile);
?>

Write to a File with PHP

To write content to a file we can use fwrite() function in PHP. To use fwrite() function to write content to a file, we first need to open the file resource in write or append mode.

$file_name = 'movies.txt';
//opens the file.txt file or implicitly creates the file
$myfile = fopen($file_name, 'w') or die('Cannot open file: '.$file_name); 
$movie_name = "The Man from Earth \n";
// write name to the file
fwrite($myfile, $movie_name);

// lets write another movie name to our file
$movie_name = "SouthPaw \n";
fwrite($myfile, $movie_name);
// close the file
fclose($myfile);

Append data to a File with PHP

If we wish to add more names to the file movies.txt then we need to open the file in append mode.

ex:-

$file_name = 'movies.txt';
//opens the file.txt file or implicitly creates the file
$myfile = fopen($file_name, 'a') or die('Cannot open file: '.$file_name); 
$movie_name = "Avengers \n";
// write name to the file
fwrite($myfile, $movie_name);

// lets write another movie name to our file
$movie_name = "Real Steel \n";
fwrite($myfile, $movie_name);
// close the file
fclose($myfile);