W e b t u t o r i a l s

Define PHP

PHP Installation

PHP INI File

PHP Case Sensitivity

PHP Variable

PHP Echo & Print

PHP Operators

PHP Receiving Input

PHP Decision Making

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP File Upload

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP-MySQL Connectivity

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP File Handling

File handling is an important part of any web application. You often need to open and process a file for different tasks. PHP has several functions for reading, writing, editing, updating files.

PHP readfile() Function

The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called "web.txt", that looks like this:

HTML = Hyper Text Markup Language
CSS = Cascading Style Sheets
PHP = Hypertext Preprocessor
SQL = Structured Query Language
Example: The PHP code to read the file.
<?php
    echo readfile("web.txt");
?>

PHP fopen() function

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function. The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened.

$myfile = fopen("web.txt", "r") or die("Unable to open file!");

PHP fread() Function

The fread() function reads text from the file. The first parameter of fread() function is the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The following PHP code reads the "web.txt" file to the end:
fread($myfile,filesize("web.txt")); 

PHP Close File - The fclose() function is used to close an open file.

fclose($myfile);

Example: The PHP code to open, read and close the file.

<?php
    $myfile = fopen("web.txt", "r") or die("Unable to open file!");
    echo fread($myfile,filesize("web.txt"));
    fclose($myfile);
?>
Check whether the file exists or not.
echo file_exists("web.txt"));

PHP fgets() function

The fgets() function is used to read a single line from a file.

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

Check End-Of-File - The feof() function checks if the end-of-file (EOF) has been reached.

<?php
    $myfile = fopen("web.txt", "r") or die("Unable to open file!");
    while(!feof($myfile)) {
        echo fgets($myfile) . "<br>";
    }
    fclose($myfile);
?>

Read fgetc() function

The fgetc() function is used to read a single character from a file.

<?php
    $myfile = fopen("web.txt", "r") or die("Unable to open file!");
    while(!feof($myfile)) {
      echo fgetc($myfile);
    }
    fclose($myfile);
?>

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

Modes Description
r Open a file for reading only.
w Open a file for writing only. Erases the contents of the file or creates a new file if it doesn't exist.
a Open a file for 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 for write only. Returns FALSE and an error if file already exists

PHP File Create/Write

The fopen() function is also used to create a file. If you use fopen() on a file that does not exist, it will create it, the file is opened for writing (w) or appending (a).

$myfile = fopen("testfile.txt", "w");

PHP Write to File - fwrite()
The fwrite() function is used to write to a file. The first parameter of fwrite() contains filename in which you want to write and the second parameter is the string to be written.

<?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "Tim Berners Lee\n";
    fwrite($myfile, $txt);
    $txt = "Hakon Wium Lie\n";
    fwrite($myfile, $txt);
    fclose($myfile);
?>