Current Location: Home> Latest Articles> PHP File Handling Tutorial: Basic to Advanced Read and Write Operations

PHP File Handling Tutorial: Basic to Advanced Read and Write Operations

gitbox 2025-06-16

1. Introduction

PHP is a widely used server-side programming language with many applications, one of which is file handling. In this article, we will explore the basic steps of PHP file handling. File operations are critical for data exchange between systems and play an important role in web development, data processing, and many other applications.

2. Reading Files

2.1 Opening a File

In PHP, before you can read or write to a file, you must first open it. The basic syntax for opening a file is as follows:


$handle = fopen("filename", "mode");
    

Here, "filename" is the name of the file you want to open, and "mode" specifies how the file will be opened. The available modes are:

  • r: Read-only mode, opens the file for reading starting at the beginning.
  • r+: Read-write mode, opens the file for reading and writing starting at the beginning.
  • w: Write-only mode, truncates the file to zero length, or creates the file if it doesn't exist.
  • w+: Read-write mode, truncates the file to zero length, or creates the file if it doesn't exist.
  • a: Write-only mode, opens the file and places the pointer at the end, or creates the file if it doesn't exist.
  • a+: Read-write mode, opens the file and places the pointer at the end, or creates the file if it doesn't exist.

After opening the file, you will get a file handle, which you will use for subsequent operations.

2.2 Reading File Content

Once the file is opened, you can use PHP's built-in functions to read its content. The basic syntax for reading file content is as follows:


$file_content = fread($handle, filesize($filename));
    

Here, $filename is the file you want to read, and $handle is the file handle you obtained when opening the file. The filesize() function is used to get the file's size, and fread() reads the specified number of bytes from the file.

If you only want to read a single line, you can use the fgets() function:


$line = fgets($handle);
    

This will read the next line from the file and move the file pointer accordingly.

2.3 Closing the File

After reading the file, it's important to close the file handle to release system resources.


fclose($handle);
    

3. Writing to Files

3.1 Opening a File

Similarly, before performing write operations on a file, you must first open the file. The basic syntax for opening a file is:


$handle = fopen("filename", "mode");
    

Where "filename" is the name of the file, and "mode" determines how the file will be opened. Common modes include:

  • w: Write-only mode, truncates the file to zero length, or creates the file if it doesn't exist.
  • w+: Read-write mode, truncates the file to zero length, or creates the file if it doesn't exist.
  • a: Write-only mode, places the file pointer at the end of the file, or creates the file if it doesn't exist.
  • a+: Read-write mode, places the file pointer at the end of the file, or creates the file if it doesn't exist.

After opening the file, you will get a file handle ($handle), which will be used for subsequent operations.

3.2 Writing to a File

Once the file is opened, you can write content to it using the fwrite() function:


fwrite($handle, $content);
    

Where $handle is the file handle and $content is the content to be written to the file.

3.3 Closing the File

After completing the write operation, it is important to close the file handle to release system resources:


fclose($handle);
    

4. Complete Example of File Reading and Writing

Here’s a complete example of reading from and writing to a file. Suppose we have a file called example.txt, which contains some text. We will read the content of the file, store it in the $old_content variable, and then append some new text to the file.


// Open the file for reading and writing
$handle = fopen("example.txt", "r+");

// Read the content of the file
$old_content = fread($handle, filesize("example.txt"));

// Append new content to the file
$new_content = "This is some new text!";
fwrite($handle, $new_content);

// Close the file
fclose($handle);
    

In this example, we open the example.txt file and store the file handle in the $handle variable. Then, we use fread() to read the file's content and store it in the $old_content variable. Next, we use fwrite() to append new text to the file. Finally, we close the file using fclose().

5. Conclusion

This article has covered the basic PHP file handling operations, including how to open, read, write, and close files. By understanding these fundamental operations, you can build powerful file-handling features for various PHP applications.