VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-append-data-to-a-file-in-php/

⇱ How to Append Data to a File in PHP? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Append Data to a File in PHP?

Last Updated : 23 Jul, 2025

Appending data to a file is a common operation in PHP when you want to add new information without overwriting the existing content. This can be particularly useful for logging, saving user activity, or adding records incrementally. We will explore different approaches to appending data to a file in PHP.

There are two main approaches to appending data to a file in PHP:

Using the fopen() and fwrite() functions

In this approach, we will open a file using fopen() in append mode ('a'), and then we will write to the file using fwrite() and then close the file using fclose() to ensure the data is saved correctly.

Example: In this example, we use fopen() and fwrite() functions to append data to a file in PHP.

Output:

👁 PHP
Output

Using file_put_contents() with FILE_APPEND flag

In this approach we will use the file_put_contents() function which is simpler and more concise method to append data. It allows us to append data to a file by passing the FILE_APPEND flag.

Example: In this example we are using file_put_contents() function to append data to a file in PHP.

Output:

👁 PHP
Output

use

Comment
Article Tags:
Article Tags: