PHP

PHP Menu

PHP

File Handling - PHP Advance

Because PHP is a server-side programming language, you may deal with files and directories on the web server. PHP has several functions for creating, reading, uploading, and editing files.

The PHP fopen() Function

The PHP fopen() function is used to open a file. The basic syntax of this function is:

fopen(filename, mode)

An example on the basic usage of the fopen() function:

<?php

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

A file may be opened in one of the following modes:

Modes Description
r Open a file - read only. File pointer starts at the beginning of the file
w Open a file - write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file - write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file - write only. Returns false and an error if file already exists
r+ Open a file - read/write. File pointer starts at the beginning of the file
w+ Open a file - read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file - read/write. The existing data in file is preserved. File pointer starts at the end of file. Creates a new file if the file doesn't exist
x+ Creates a new file - read/write. Returns false and an error if file already exists

The PHP fclose() Function

The fclose() function is used to close the file, as shown below:

<?php

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

The PHP fread() Function

The basic syntax of the fread() function is:

fread(file handle, length in bytes)

This function takes two parameter — A file handle and the number of bytes to read. Below is an example of the fread() function used to read from a file, myfile.txt with the following content: "The quick brown fox jumps over the lazy dog."

<?php

$f = "myfile.txt";
             
// Check the existence of file
if (file_exists($f)) {
    $file = fopen($f, "r") or die("ERROR: Cannot open the file."); // Open the file for reading             
    $content = fread($file, "20"); // Read fixed number of bytes from the file             
    fclose($file); // Closing the file 
    // Display the file content 
    echo $content; // Output: "The quick brown fox"
} else {
    echo "ERROR: File does not exist.";
}

The PHP filesize() Function

The filesize() function returns the size of the file in bytes. Using this function together with the fread() function, reads the entire content of the file.

<?php

$f = "myfile.txt";
             
// Check the existence of file
if (file_exists($f)) {
    $file = fopen($f, "r") or die("ERROR: Cannot open the file."); // Open the file for reading              
    $content = fread($file, filesize($f)); // Reading the entire file
    fclose($file); // Closing the file
    // Display the file content
    echo $content; // Output: "The quick brown fox jumps over the lazy dog."
} else {
    echo "ERROR: File does not exist.";
}

The PHP readfile() Function

The readfile() function allows reading the contents of a file without needing to open it. Below is an example:

<?php

$f = "myfile.txt";
             
// Checking the existence of file
if (file_exists($f)) {
    readfile($f) or die("ERROR: Cannot open the file."); // Reads and outputs the entire file
} else {
    echo "ERROR: File does not exist.";
}

The PHP file_get_contents() Function

The file_get_contents() function accepts the name and path to a file, and reads the entire file into a string variable. This is another way to read the whole content of a file without needing to open it.

<?php

$f = "myfile.txt";
             
// Check the existence of file
if (file_exists($f)) {
    $content = file_get_contents($f) or die("ERROR: Cannot open the file."); // Reading the entire file into a string               
    echo $content; // Display the file content 
} else {
    echo "ERROR: File does not exist.";
}

The PHP file() Function

The file() function returns the file contents as an array of lines. This is similar to the file_get_contents() function. Below is an example:

<?php

$f = "myfile.txt";
             
// Check the existence of file
if (file_exists($f)) {
    // Reading the entire file into an array
    $content = file($f) or die("ERROR: Cannot open the file.");
    foreach($content as $line){
        echo $line;
    }
} else {
    echo "ERROR: File does not exist.";
}

The PHP fwrite() Function

Using the PHP fwrite() function, you can write data to a file or add to an existing file. The basic syntax is:

fwrite(file handle, string)

This function takes two parameters - the file handle and the string of data that is to be written. Below is an example:

<?php

$f = "myfile.txt";
    
$content = "The quick brown fox jumps over the lazy dog."; // String of data to be written

$file = fopen($f, "w") or die("ERROR: Cannot open the file."); // Open the file for writing

fwrite($file, $content) or die ("ERROR: Cannot write to file."); // Write data to the file

fclose($file); // Closing the file

echo "Data written successfully.";

The PHP rename() Function

The rename() function modifies the name of the file. Below is an example on how to rename a file.

<?php

$f = "myfile.txt";
             
// Check the existence of file
if (file_exists($f)) {
    // Attempt to rename the file
    if (rename($f, "newfile.txt")) {
        echo "Rename successful.";
    } else {
        echo "ERROR: File cannot be renamed.";
    }
} else {
    echo "ERROR: File does not exist.";
}

The PHP unlink() Function

The unlink() function delete files or directories. Below is an example of removing/deleting a file using this function.

<?php

$f = "myfile.txt";
             
// Check the existence of file
if (file_exists($f)) {
    // Attempt to delete the file
    if (unlink($f)) {
        echo "File deleted successfully.";
    } else {
        echo "ERROR: File cannot be deleted.";
    }
} else {
    echo "ERROR: File does not exist.";
}

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods