File handling in PHP, allows to work with files and directories and its related operations like to create, write, append, modify, read and delete files.

PHP provides following in-built File handling functions

fopen()Opens an existing file.
fclose()Closes a file.
fread()Reads data from a file.
fwrite()Writes data to a file.
file_exists()Checks if a file exists.
unlink()Deletes an existing file.

Open a file

A file needs to be open first, before reading, writing and modifying. PHP provides fopen() function to open a file.

fopen syntax

fopen(filename, mode);

Example

<?php

// Open a file in read mode
$file = fopen("myfile.txt", "r"); 

if ($file) {
    echo "File opened successfully!";
} else {
    echo "Failed to open the file.";
}

?> 

Close a file

When a file is opened in PHP, it must be closed. PHP provide fclose() to close an opened file.

fclose() takes on argument. which is fopen() variable.

<?php

// Open the file in read mode
$file = fopen("myfile.txt", "r"); 

if ($file) {
    echo "File successfully opened";
}
fclose($file); // Closes the file
?>

File modes

wOpens a file in writing mode.
It creates a new file, if file not exists.
It overwrite existing contents of file.
rOpens file in read mode.
aAppend mode, This mode adds content to existing file contents. Do not overwrite file.
w+Same as w mode, allows file reading and writing.
r+File reading and writing mode.
a+File reading and append mode.
xCreate a new file in writing mode.
x+Creates a new file for read and write.

Reading a file

PHP provides following functions to read files

fread()reads entire file content
file_get_contents()same as fread
fgets()Read file line by line
fgetc()Reads single character from file
feof()represent end of file

fread()

Below Example reads the entire file content.

<?php

$f = fopen("myfile.txt", "r");
$txt = fread($f, filesize("myfile.txt"));

echo $txt;
fclose($f);

?>

Read file Line by line

fgets() function reads file line by line.

<?php
  
$f = fopen("myfile.txt", "r");

if ($f) {
    while (($ln = fgets($f)) !== false) {
        echo $ln . "<br>";
    }
    fclose($f);
}

?>

Read Character by Character from file

fgetc() reads a single character, use a loop to read an entire file character by character.

<?php
$f = fopen("myfile.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($f)) {
  echo fgetc($f);
}
fclose($f);
?>

Write a file

fwrite() in PHP allows to write in an opened file. To write in a file it should be opened in “w” (write) or “a” (append) mode.

<?php
$f = fopen("myfile.txt", "w") or die("Unable to open file");
$txt = "PHP\n";
fwrite($f, $txt);
$txt = "Javascript\n";
fwrite($f, $txt);
$txt = "HTML\n";
fwrite($f, $txt);

fclose($f);
?>

Append a file

To add data to an already existing file, a file should be opened in “a” (append) mode.

<?php
$f = fopen("myfile.txt", "a") or die("Unable to open file");
$txt = "CSS\n";
fwrite($f, $txt);
$txt = "Jquery\n";
fwrite($f, $txt);

fclose($f);
?>

Deleting Files in PHP

unlink() function is used to delete a file in PHP.

<?php

if (!unlink("myfile.txt"))
    echo ("File not deleted, due to error");
else
    echo ("File successfully deleted");
?>