Introduction
File handling is a crucial part of backend development, especially when working with dynamic data, logs, and file-based storage systems. PHP provides powerful built-in functions to manage files efficiently. Among these, fopen(), fread(), and fwrite() are the most commonly used functions for opening, reading, and writing files.
What is fopen(), fread(), fwrite()?
1. fopen()
fopen() is used to open a file in a specific mode. It returns a file pointer resource that is used by other file functions.
2. fread()
fread() is used to read data from an open file. It reads a specified number of bytes from the file.
3. fwrite()
fwrite() is used to write data into a file. It can create new content or overwrite existing content depending on the file mode.
These three functions work together in most file operations:
Open file → Read/Write → Close file
Syntax
fopen() Syntax
$file = fopen("filename.txt", "mode");
Common Modes:
- “r” → Read only
- “w” → Write (overwrite file)
- “a” → Append data
- “x” → Create new file
- “r+” → Read & write
$content = fread($file, filesize("filename.txt"));
fwrite() Syntax
fwrite($file, "Your text here");
Closing File
fclose($file);
Examples:
Example 1: Using fopen() and fwrite()
$file = fopen("demo.txt", "w");
fwrite($file, "Hello, this is PHP file handling!");
fclose($file);
echo "Data written successfully!";
Example 2: Using fopen() and fread()
$file = fopen("demo.txt", "r");
$content = fread($file, filesize("demo.txt"));
echo $content;
fclose($file);
Example 3: Append Data using fwrite()
$file = fopen("demo.txt", "a");
fwrite($file, "\nThis line is appended.");
fclose($file);
Example 4: Complete Flow (Open → Write → Read)
$file = fopen("sample.txt", "w");
fwrite($file, "Learning PHP file functions.");
fclose($file);
$file = fopen("sample.txt", "r");
echo fread($file, filesize("sample.txt"));
fclose($file);
Real-Life Example
Example: Website Logging System
In real-world applications, developers use these functions to track user activities.
$file = fopen("activity_log.txt", "a");
$log = "User visited page at: " . date("Y-m-d H:i:s") . "\n";
fwrite($file, $log);
fclose($file);
Explanation:
- fopen() opens the log file in append mode
- fwrite() writes new log data
- fclose() closes the file
Example: Saving Form Data
$name = "Gaurav";
$email = "gaurav@email.com";
$file = fopen("users.txt", "a");
$data = "Name: $name, Email: $email\n";
fwrite($file, $data);
fclose($file);
This is useful for small applications where a database is not required.
Common Mistakes
1. Not Closing the File
Leaving files open can cause memory issues.
❌ Wrong:
$file = fopen("file.txt", "r");
✔️ Correct:
fclose($file);
2. Using Wrong Mode
Using “w” instead of “a” deletes existing content.
3. Not Checking File Existence
Trying to read a file that doesn’t exist.
✔️ Solution:
if(file_exists("file.txt")) {
$file = fopen("file.txt", "r");
}
4. Ignoring Errors
Not handling file opening errors.
✔️ Best Practice:
$file = fopen("file.txt", "r") or die("Unable to open file!");
5. Incorrect File Size in fread()
Using wrong size may result in incomplete data.
Conclusion
The functions fopen(), fread(), and fwrite() are the backbone of file handling in PHP. They allow developers to efficiently manage files by opening, reading, and writing data as needed.
These functions are widely used in real-world applications such as logging systems, data storage, and file-based content management. By understanding how to use them correctly, developers can build more flexible and efficient applications.