Current Location: Home> Latest Articles> PHP File I/O Operations Explained: A Complete Guide to Efficient Reading and Writing

PHP File I/O Operations Explained: A Complete Guide to Efficient Reading and Writing

gitbox 2025-06-15

1. Introduction

PHP is a widely used server-side scripting language for web development, supporting object-oriented programming and various databases. In PHP development, file I/O operations are an indispensable part. This article will explain how to efficiently implement file reading and writing functionalities in PHP.

2. File Reading

2.1 Opening a File with fopen

In PHP, the first step to read a file is to open it using the fopen() function. The fopen() function accepts two parameters: the first is the file name, and the second is the mode in which the file is opened. Common modes include 'r' (read-only), 'w' (write), 'a' (append), etc. Here's a simple example:

$file = fopen("example.txt", "r") or die("Unable to open file!");
    

This code opens the file "example.txt" in read-only mode.

2.2 Reading a File Line by Line

Once the file is opened, you can use the fgets() function to read the contents line by line. The fgets() function takes one parameter: the file handle. Here's an example:

while (!feof($file)) {
    $line = fgets($file);
    echo $line . "";
}
    

This code will read the content of the file line by line and output it.

2.3 Reading the Entire File

In addition to reading the file line by line, you can use the fread() function to read the entire file at once. The fread() function accepts two parameters: the file handle and the number of bytes to read. Here's an example:

$filesize = filesize("example.txt");
$content = fread($file, $filesize);
echo $content;
    

This code will read the entire content of "example.txt" and output it.

3. File Writing

3.1 Opening a File with fopen

The first step to writing a file is to open it with the fopen() function. Like reading files, you can open a file using 'r', 'w', or 'a' modes.

$file = fopen("example.txt", "w") or die("Unable to open file!");
    

This code opens the file "example.txt" in write mode.

3.2 Writing to a File

Once the file is open, you can use the fwrite() function to write content to it. The fwrite() function accepts two parameters: the file handle and the content to write. Here's an example:

$file = fopen("example.txt", "w") or die("Unable to open file!");
$txt = "Hello World!";
fwrite($file, $txt);
fclose($file);
    

This code writes "Hello World!" to the file "example.txt".

3.3 Appending to a File

Sometimes, you may want to append new content to an existing file instead of overwriting it. You can do this by opening the file in 'a' mode. Here's an example:

$file = fopen("example.txt", "a") or die("Unable to open file!");
$txt = "New content!";
fwrite($file, $txt);
fclose($file);
    

This code will append "New content!" to the file "example.txt".

4. Conclusion

File reading and writing are common and useful tools in web development with PHP. In this article, we covered the basics of file I/O operations using commonly used functions like fopen, fgets, fread, and fwrite, helping you to master file handling in PHP. We hope this article has been helpful!