PHP File Handling

by Vincy. Last modified on July 2nd, 2022.

Using PHP file handling mechanism, we can get external file resources to store as a reference. We have slightly touched on file resources while discussing PHP Resource Data.

PHP provides set of in-built functions to handle files. Some of the functions are, fopen(), file_exists(), file_get_contents() and etc.

Some of the basic operations to deal with files are listed below.

  • Opening file
  • Working with file read, write and append
  • Closing file

Opening file

To open a file, we need to specify the name of that file and mode. The mode of operation deals with the access limitation of the file to be opened.

Mode of Operation

In PHP file handling, there are four sets of possible modes. These are,

  • {r and r+} – To read the existing files.
  • {w and w+} – To change the entire file content.
  • {a and a+} – To add content after existing file content.
  • {x and x+} – To create a new file and work with it.

Read existing file.

In PHP, the following types of two file handling modes are used to read the existing files. The file pointer will point to the start of the file.

Mode of Operation File Mode File Pointer Position
r read-only Start of the file content
r+ read-write Start of the file content

Using fopen() function, we can open a file by its name and corresponding mode. For example,

<?php
$filePointer = fopen("hello.txt", "r");
while (! feof($filePointer)) {
    echo fgets($filePointer) . "<br>";
}
?>

Note: We should ensure that the specified file exists. If not, the following error will occur,

Warning: fopen(hello.txt) [function.fopen]: failed to open stream: No such file or directory in ...

Warning: fgets() expects parameter 1 to be resource, boolean given in ...

Write into a file.

To perform the write operation, we have two choices to select the mode of the file to be opened. These are,

Mode of Operation File Mode File Pointer Position
w write-only Start of the file content
w+ read-write Start of the file content

By using these modes, the entire file content will be cleared and the pointer will focus the start position of the file content. This method is used to change the existing file content, completely.

For example,

<?php
$filePointer = fopen("hello.txt", "w");
fwrite($filePointer, "PHP POT: MySQL Query\n");
?>

Append content to the file

In this operation, the existing content of the file will not be cleared. Rather, we can add new content continuously. Here, file pointer will point end of the file. And the possible mode of operations are,

Mode of Operation File Mode File Pointer Position
a write-only Start of the file content
a+ read-write Start of the file content

The code will be similar to that of file-write except for the mode specified to open the file. For example,

<?php
$filePointer = fopen("hello.txt", "a");
?>

Note: In PHP, write and append mode will open the specified file if exists. Otherwise, a new file will be created for performing file write and append.

create a new file

Here also two file handling modes available that are used to write into a newly created file.

Mode of Operation File Mode File Pointer Position
x write-only.
x+ read-write.

For example,

<?php
$filePointer = fopen("hello.txt", "x");
fwrite($filePointer, "PHP POT: FUNCTIONS\n");
?>

Caution: is already exists, then the execution will be stopped with the following error.

Warning: fopen(hello.txt) [function.fopen]: failed to open stream: File exists in ... on line ...

Warning: fwrite() expects parameter 1 to be resource, boolean given in ... on line ...

Closing File

After performing all the above file operations of PHP, we need to close it by using the fclose() function. fclose() function should hold the file pointer which is the reference for the file resources. In PHP, we can use fclose() as shown below.

<?php
fclose($filePointer);
?>
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page