Performing File Operations in PHP

In this article, I will discuss Performing File Operations in PHP.

Since file handling is an important feature in many applications. So, all programming languages offer a library of in-built functions for file handling. As a matter of fact, PHP provides a number of methods to create, read, and manipulate files and directories. The following list displays the functions that we can use in file handling. For detailed information on file functions in PHP, you can refer to PHP Manual.

  • fopen()
  • fread()
  • write()
  • fgetc()
  • fgets()
  • fclose()
  • readfile()
  • feof()

Brief Introduction of the Functions for Performing File Operations in PHP

To illustrate the usage of these functions, a number of examples are also included. To begin with, let us discuss the fopen() function. Since, we use the fopen() function to open a file in a particular mode, this function is required along with other functions for file manipulation.

Basically, the function fopen() opens a file in the specified mode and returns a file handle. Further, we use this file handle to create, read, or append a file. The following example demonstrates creating a file with fopen() function called with write-mode.

Using fopen() in write mode to create a file

Likewise, the function fread() allows us to read and display the content of a file if the file is opened in the read mode.

Similarly, the function fwrite() is used to write the content in a file depending upon the file mode. Therefore, we can either overwrite a file or append the content to an existing file. The following example demonstrates the use of write-only and read-write file modes.

Difference between w and w+ modes

Furthermore, both the fopen(), and fwrite() functions can be used to append the content to an existing file. Basically, we use the file modes ‘a’, and ‘a+’ for this purpose. The following example illustrates it.

Difference between a and a+ File Modes

Performing File Operations in PHP – Reading a Single Character or a Line

Additionally, PHP offers two more functions that allow us to read a specific character or a specific line in a file. These functions are fgetc() and fgets() respectively. While the fgetc() function reads a character from a file. In contrast, the fgets() function reads a line. However, it is possible to read the whole file using a loop with any of these two functions. In fact, there is a function called feof() that indicates whether the end of file is reached or not.

The fgetc() function reads a single character of the file. To illustrate, consider the following example.

Reading a File Character By Character

The fgets() function reads a line in a file. To illustrate, consider the following example for the usage of the fgets() function.

Reading the file line by line using the fgets() function

Finally, we use the function fclose() to close an open file.

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP


You may also like...