PHP File Handling 

File handling is most important part of PHP web application. You often need to open and use a file for different tasks.

Not all web applications use database, so storeing simple data in text files is a good option. With files in PHP you have some types of Operations can perform.

  • fopen() – Opening a file
  • fread() – Reading a file
  • fwrite() – Writing to a file
  • fclose() – Closing a file
  • unlink() – Deleting a file

PHP Open File – fopen()

PHP provides the fopen () method to open files. In this method two arguments are passed. The first argument is the name of the file, and the second mode is what you want to open it in.

Can open PHP file in several modes. All these modes are represented by a character. All these modes are used to perform operations in different ways.

All these modes are being given below.

Modes Description
wThis mode can be written in files only. If the file is not already exists, then it is created.
w+This mode can be used to read / write both operations with file.
aThis mode does not delete the contents of the old file but adds the new content to it.
a+In this mode you can also read the file and it can also append the data.
rFiles can only be read from this mode.
r+From this mode, the file can also be read and written in that file can also be done.
xWith this mode you can write data in the file. The new file is created. But if the file is already there, the error will show.
x+With this mode you can both read and write.

Note:- To perform any type of operation with the file, it is necessary to first open the file.

Example

<?php
$myfile = fopen("filename.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("filename.txt"));
fclose($myfile);
?>

PHP Read File – fread()

To read the file, you have to use two functions. For this, you use the fread () function. In this function you file stream With its length pass.

File stream is returned by the fopen () method. fread () method returns the contents of file as string but You must use the echo statement to print.

The length of the file is passed, so that the compiler knows that the file contains content. For this, you use the filesize () method. This method returns the file size as integer. In this method, you pass the name of the file.

Example

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    // Read fixed number of bytes from the file
    $content = fread($handle, "20");
        
    // Closing the file handle
    fclose($handle);
        
    // Display the file content 
    echo $content;
} else{
    echo "ERROR: File does not exist.";
}
?>

PHP Write to File – fwrite()

To store data in any file, you use the fwrite () function. In this function, file stream and data are passed. File stream is returned by the fopen () method.

Example

<?php
$file = "note.txt";
    
// String of data to be written
$data = "The quick brown fox jumps over the lazy dog.";
    
// Open the file for writing
$handle = fopen($file, "w") or die("ERROR: Cannot open the file.");
    
// Write data to the file
fwrite($handle, $data) or die ("ERROR: Cannot write the file.");
    
// Closing the file handle
fclose($handle);
    
echo "Data written to the file successfully.";
?>

PHP Close File – fclose()

You should close the file stream after the operation is complete. For this, PHP provides fclose () method. In this method, file pointer pass is passed.

Example

<?php
$file = "data.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Open the file for reading
    $handle = fopen($file, "r") or die("ERROR: Cannot open the file.");
        
    /* Some code to be executed */
        
    // Closing the file handle
    fclose($handle);
} else{
    echo "ERROR: File does not exist.";
}
?>

PHP Removing file – unlink()

After completing the work, if you want to delete the file, PHP provides the unlink() method.

Example

<?php
$file = "note.txt";
 
// Check the existence of file
if(file_exists($file)){
    // Attempt to delete the file
    if(unlink($file)){
        echo "File removed successfully.";
    } else{
        echo "ERROR: File cannot be removed.";
    }
} else{
    echo "ERROR: File does not exist.";
}
?>