File Handling in PHP (Read, Write, Append)
In this lesson: Read, write and append files in PHP with fopen, fwrite, fgets, file_get_contents and file_put_contents — code and output.
Why file handling
File handling lets PHP save data to a file and read it back later — useful for logs, simple settings, exports, or storing text when you do not need a full database. PHP makes the common cases very easy with two functions: file_put_contents and file_get_contents.
Write to a file
file_put_contents creates the file (if needed) and writes text into it.
<?php
file_put_contents("data.txt", "Hello CodingEasily");
echo "Saved!";
?>
Warning: this replaces everything already in the file. To keep old content and add to it, use append (next).
Read a file
<?php
$text = file_get_contents("data.txt");
echo $text;
?>
file_get_contents returns the whole file as one string. Simple and perfect for small files.
Append to a file
Add the FILE_APPEND flag to add to the end instead of replacing.
<?php
file_put_contents("log.txt", "Line 1\n");
file_put_contents("log.txt", "Line 2\n", FILE_APPEND);
echo file_get_contents("log.txt");
?>
Line 2
Without FILE_APPEND, "Line 2" would have erased "Line 1". Appending is how you build up logs.
fopen for line-by-line
For big files, open a handle and read line by line so you do not load everything at once.
<?php
$f = fopen("log.txt", "r"); // "r" = read mode
while (($line = fgets($f)) !== false) {
echo $line;
}
fclose($f);
?>
Line 2
Always fclose() when done. Common modes: "r" read, "w" write (erase), "a" append.
Check and delete
<?php
if (file_exists("data.txt")) {
echo "File is there";
// unlink("data.txt"); // deletes the file
}
?>
file_exists checks before you read or delete. unlink removes a file — use it carefully.
Common mistakes
- Overwriting instead of appending. Plain
file_put_contentsreplaces; addFILE_APPENDto keep old data. - Wrong path. A relative path is based on the script's location — check it if the file is not found.
- Permission errors. The folder must be writable by the web server.
- Forgetting
fclose(). Always close a file you opened withfopen.
Summary
file_put_contentswrites;file_get_contentsreads a whole file.- Add
FILE_APPENDto add to the end instead of replacing. - Use
fopen+fgetsfor large files, line by line, thenfclose. file_existschecks;unlinkdeletes.- For lots of structured data, use MySQL instead of files.
Next, store structured data in MySQL, or export data with PHP CSV export.
Frequently Asked Questions
What is the easiest way to write to a file in PHP?
file_put_contents("data.txt", $text). It creates the file if it does not exist and writes your text in one line. It is the simplest way for small files.How do I read a whole file in PHP?
file_get_contents("data.txt"). It returns the entire file content as a string. For reading line by line, use fopen with fgets in a loop.What is the difference between write and append?
file_put_contents($f, $text, FILE_APPEND) to append.Why does my file write fail?
When should I use fopen instead of file_get_contents?
fopen when the file is large or you want to process it line by line, so you do not load the whole file into memory at once. For small files, file_get_contents is simpler.