Current Location: Home> Latest Articles> Comprehensive Guide to PHP File Reading and Writing Methods

Comprehensive Guide to PHP File Reading and Writing Methods

gitbox 2025-06-06

PHP File Handling Guide: Practical Methods for Reading and Writing Files

In PHP development, working with file input and output is a common and essential task. Whether you're storing data, logging activity, or managing configurations, mastering file operations can greatly enhance your programming efficiency. This guide explores the key functions for reading from and writing to files in PHP.

1. File Reading in PHP

PHP provides several built-in functions to read files, including fopen(), fread(), fgets(), fgetcsv(), and fclose(). Here's how to use them effectively:

1.1 Using fopen() to Open Files

The fopen() function opens a file and returns a file handle. It takes two parameters: the file path and the mode in which to open the file.

Common file open modes include:

  • r: Read-only, pointer at the beginning of the file
  • r+: Read and write, pointer at the beginning of the file
  • w: Write-only, creates the file if it doesn’t exist, clears contents if it does
  • w+: Read and write, creates the file if it doesn’t exist, clears contents if it does
  • a: Write-only, creates the file if it doesn’t exist, appends to the end
  • a+: Read and write, creates the file if it doesn’t exist, appends to the end
$filename = 'example.txt';
$handle = fopen($filename, 'r') or die('Unable to open file!');

1.2 Using fread() to Read Content

The fread() function reads a specified number of bytes from an open file. It’s often used in combination with filesize() to read the entire file content.

$filename = 'example.txt';
$handle = fopen($filename, 'r') or die('Unable to open file!');
$content = fread($handle, filesize($filename));
echo $content;
fclose($handle);

This code reads the entire content of the example.txt file and displays it.

1.3 Using fclose() to Close Files

The fclose() function is used to close a file handle opened by fopen(), freeing up system resources.

$filename = 'example.txt';
$handle = fopen($filename, 'r') or die('Unable to open file!');
$content = fread($handle, filesize($filename));
fclose($handle);

2. Writing to Files in PHP

Besides reading, PHP also supports powerful file writing capabilities. Common functions include fwrite() and fputs().

2.1 Writing with fwrite()

The fwrite() function writes a string to an open file. You must open the file with fopen() using the appropriate mode, such as 'a+' for appending.

$filename = 'example.txt';
$handle = fopen($filename, 'a+') or die('Unable to open file!');
$newContent = "This is the new content.";
fwrite($handle, $newContent);
fclose($handle);

This appends the new content to the end of the file.

2.2 Writing with fputs()

The fputs() function functions exactly like fwrite() and is used interchangeably.

$filename = 'example.txt';
$handle = fopen($filename, 'a+') or die('Unable to open file!');
$newContent = "This is the new content.";
fputs($handle, $newContent);
fclose($handle);

Just like fwrite(), fputs() writes content to the end of the file when opened in append mode.

3. Summary

File reading and writing are fundamental operations in PHP. Functions like fopen(), fread(), fwrite(), fputs(), and fclose() allow developers to efficiently manage file-based data. Understanding how and when to use these functions ensures more robust and maintainable PHP applications.