Current Location: Home> Latest Articles> PHP File Read and Write Tutorial: Basic Operations and Practical Examples

PHP File Read and Write Tutorial: Basic Operations and Practical Examples

gitbox 2025-06-10

1. What is File Read and Write Operation

File read and write operations refer to reading from and writing to files using a computer. Simply put, programs can operate on the computer's file system through programming languages, enabling opening, reading, writing, and closing files.

2. Opening and Closing Files

In PHP, the fopen function is used to open files, and fclose is used to close files. Each opened file should be closed promptly to avoid wasting system resources.

2.1 Opening Files

When opening a file, you need to specify the file path and the mode. The file path can be relative or absolute. Common opening modes include:


$file_handle = fopen("file_path", "mode");

Examples of modes:

  • r: Read-only mode, the file must exist
  • w: Write mode, clears existing content if the file exists, creates the file if it doesn't
  • a: Append mode, preserves existing content and writes new data to the end of the file

2.2 Closing Files

After completing operations, use fclose to close the file and free resources:


fclose($file_handle);

3. File Read and Write Operations

Once a file is opened, you can read from or write to it.

3.1 File Writing Operations

PHP supports two ways to write data: overwrite existing content or append to it.

3.1.1 Overwrite Writing

Use fwrite to write content while opening the file in w mode, which overwrites existing content:


$file_handle = fopen("file_path", "w");
fwrite($file_handle, "Hello World!");
fclose($file_handle);

Notes:

  • fwrite's first argument is the file handle, second is the content to write, and the optional third specifies how many bytes to write; by default, it writes all content.
  • Using w mode clears existing content if the file exists or creates a new file if it doesn't.

3.1.2 Append Writing

Open the file in append mode (a) to add content at the file's end:


$file_handle = fopen("file_path", "a");
fwrite($file_handle, "Hello World!");
fclose($file_handle);

Note: If the file doesn't exist, the a mode will create it automatically.

3.2 File Reading Operations

Use fgets to read a file line by line:


$file_handle = fopen("file_path", "r");
while (!feof($file_handle)) {
  $line = fgets($file_handle);
  // Process $line
}
fclose($file_handle);

Explanation:

  • fgets reads one line at a time and moves the pointer to the next line automatically.
  • feof checks if the end of the file has been reached.

4. Error Handling

File operations may encounter errors like missing files or insufficient permissions, which require proper handling.

4.1 Error Handling Functions

PHP offers die and exit functions to output error messages and terminate script execution.

4.2 Error Handling Example


$file_handle = fopen("file_path", "r");
if (!$file_handle) {
  die("Unable to open file");
}
while (!feof($file_handle)) {
  $line = fgets($file_handle);
  // Process the line
}
fclose($file_handle);

If the file cannot be opened, the script outputs "Unable to open file" and stops executing.

5. Summary

This article introduced the basic PHP file read and write operations, including opening and closing files, writing and reading file content, and common error handling techniques, helping you efficiently manage files in PHP.