PHP File Handling methods with suitable Examples

In a Web application generally we use File to store data. Assume that in your CMS you want to generate HTML files for each content you like to publish. Here you required File Operations. Before move into the basics of File Operations using PHP let you recall every File has its own properties like File Name, File Size, File Type or Created Date. File to File depending upon the header information File type varies. In PHP to get started with File Operations first you need to learn the following PHP File Handling methods.

Methods Description
fopen() In PHP using fopen() method we can open a file. fopen() accepts 2 parameters. First parameter is the File Name and second parameter is the mode. There are 6 types of modes r (Open file in read-only mode), r+ (Open the file for writing and reading), w (Open the file for write only mode), w+ (Open the file for reading and writing), a (Open the file for writing only), a+ (Open the file for reading and writing only).
fread() Using fopen() once we open the file to read the contents we use fread(). The method fread() acts like a pointer. It accepts 2 parameters.
fwrite() Using this method we can write or append a File. To execute fwrite() first it is mandatory to open the File. fwrite() accepts 2 parameters. First parameter is the File pointer and second parameter is string of data you want to write in the File.
fclose() Use to close File. fclose() operators exactly opposite to fopen() method.
filesize() Using this method we can retrieve the Size of a File.
readfile() This method we use to read a file. Compare to fopen() readfile() writes the output to buffer.
unlink() This method we use to delete a File.

Create a File using PHP

To Create a File in PHP we use fopen() method with writing mode (w). Look at the below example how I am Creating a text file “demo.txt” using fopen() method.

<?php
$tempFile = 'demo.txt';
$handle = fopen($tempFile, 'w') or die('Unable to Open file:  '.$tempFile);
?>

Open & Read a File

In this example I am using fopen to open and fread to read the file. To run the below example Create a text file (sample.txt) with few lines of Contents. Then use open and read methods.

$tempFile = 'sample.txt';
$handle = fopen($tempFile, 'r');
$data = fread($handle,filesize($tempFile));
echo $data;

Also using readfile() you can read a file in PHP. This method is so simple to implement. Look at the example below.

<?php
echo readfile("sample.txt");
?>

Example to Write or Append a File in PHP

In PHP to write contents into a file first we required to open the file. Once the file opened successfully we can use fwrite() method to write contents. In the below example I am writing 2 lines to a text file.

$tempFile = 'demo.txt';
$handle = fopen($tempFile, 'a') or die('Unable to Open file:  '.$tempFile);
$data = 'This is the First line.';
fwrite($handle, $data);
$new_line = "\n".'This is the Second line I am appending.';
fwrite($handle, $new_line);

Close or Delete a File in PHP

As a principle of PHP File handling make practice to Close a file after did with the required operations. To close a file we use fclose(). Look at the below example.

$tempFile = 'demo.txt';
$handle = fopen($tempFile, 'a') or die('Unable to Open file:  '.$tempFile);
$data = 'I am working at InfoSys.';
fwrite($handle, $data);
fclose($handle);

Similarly to delete a file we use unlink() method. Assume that you have a text file in root folder. To delete that you can go for the below line codes.

$tempFile = 'demo.txt';
unlink($tempFile);