Creating simple text files using PHP
In PHP, there are generally two common ways to store data: use a database like MySQL, or store the data in flat files—usually plain text like .txt or .json. This is known as flat file storage.
Flat file storage can be a great choice for small projects, quick features, or scripts that don't need the overhead of a database. It's easy to implement and doesn't require server-side database setup. However, plain text files are not secure and should never be used for sensitive data like login credentials.
If you must store credentials or config values in a file, use a .php file that returns an array, not a publicly accessible .txt or .json file. See the related tutorials: Storing data in PHP files and Storing data in JSON files.
What is flat file storage?
Flat file storage means storing your data in a simple file—usually as plain text. This is useful when:
- - You don't want to deal with a database
- - The data isn't sensitive
Common flat file formats include .txt and .json. This tutorial focuses on storing plain text.
Creating and writing a file (basic)
Let's start simple. PHP has fopen() and fwrite() for creating and writing to files:
<?php
$filename = 'data.txt';
$content = "Hello, world!\n";
// Open file in append mode
$handle = fopen($filename, 'a');
if ($handle) {
fwrite($handle, $content);
fclose($handle);
} else {
echo "Error: Unable to open file.";
}
In this case, 'a' mode means the content will be appended to the end of the file if it already exists.
Useful modes for fopen()
When using fopen(), you can specify different file modes to control how the file is accessed—whether you're writing, appending, or reading and writing. Here are a few of the most common ones:
'w': Write (overwrites the file if it already exists)
'a': Append (adds content to the end)
'r+': Read and write (doesn't delete existing content)
Choose the mode based on what you're trying to do. For example, use 'a' if you're adding data to existing content, or 'w' if you're replacing everything in the file.
Writing safely with LOCK_EX
To prevent issues when multiple users edit a file at the same time, use flock() with LOCK_EX while writing. Also remember to always close the file.
This protects your data from getting corrupted or overwritten by another script during the same moment.
Final example (safe writing)
This version adds proper file locking and a check to see if the file is writable before writing:
<?php
$filename = 'data.txt';
$content = "Hello, world!\n";
// Check if file is writable or doesn't exist yet
if (is_writable($filename) || !file_exists($filename)) {
$handle = fopen($filename, 'a');
if ($handle && flock($handle, LOCK_EX)) {
fwrite($handle, $content);
flock($handle, LOCK_UN);
fclose($handle);
} else {
echo "Unable to write to file.";
}
} else {
echo "File is not writable.";
}
Tip: If you need to clear the file before writing, switch the mode to 'w' instead of 'a'.
Reading the file
To read a flat text file, you can use file_get_contents() or fgets() in a loop:
<?php
$filename = 'data.txt';
// Check if file exists
if (file_exists($filename)) {
// Read file
$data = file_get_contents($filename);
// Output data
echo nl2br(htmlspecialchars($data));
} else {
echo "File not found.";
}
htmlspecialchars() ensures that special characters are escaped, and nl2br() preserves line breaks in the browser.
Updating or deleting data
Since flat text files don't have structure like arrays or objects, you can't directly update individual "values" unless you parse or rewrite the entire content. But here's how to replace the file content with something new:
<?php
$filename = 'data.txt';
$newContent = "This is the updated content.\n";
// Check if file is writable or doesn't exist yet
if (is_writable($filename) || !file_exists($filename)) {
$handle = fopen($filename, 'w');
if ($handle && flock($handle, LOCK_EX)) {
fwrite($handle, $newContent);
flock($handle, LOCK_UN);
fclose($handle);
} else {
echo "Unable to write to file.";
}
} else {
echo "File is not writable.";
}
And to delete the entire file:
<?php
$filename = 'data.txt';
// Check if file exists
if (file_exists($filename)) {
unlink($filename);
echo "File deleted.";
} else {
echo "File does not exist.";
}
If you actually want to store structured data that you can edit and delete properly, read the related tutorials Storing data in PHP files and Storing data in JSON files for that! ✨