PHP Writing Files
In this page:
$handle = fopen("filename.txt", "w"); // "a" appends instead
fwrite($handle, $data);
fclose($handle);
file_put_contents("filename.txt", $data, FILE_APPEND);
Writing with fwrite()
fopen($path, w) opens a file for writing, and importantly truncates any existing content first — if you want to add to a file instead of replacing it, use a (append) mode instead.
Example: Writing with fwrite()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Call `fwrite($handle, "First write")`
fwrite($handle, "First write");
// Call `fclose($handle)`
fclose($handle);
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>
Login to try C/C++/Java/PHP code in the editor
Appending with fwrite()
fwrite($handle, $data) writes a string to an open file handle, returning the number of bytes actually written, which is worth checking against the length of your data on systems where disk space might run out.
Example: Appending with fwrite()
<?php
// Declare `$handle`, set to `fopen("data.txt", "a")`
$handle = fopen("data.txt", "a");
// Declare `$bytes`, set to `fwrite($handle, "Appended data")`
$bytes = fwrite($handle, "Appended data");
// Call `fclose($handle)`
fclose($handle);
// Print "$bytes bytes written" to the output
echo "$bytes bytes written";
?>
Login to try C/C++/Java/PHP code in the editor
Simple Writes with file_put_contents()
file_put_contents($path, $data) writes a string to a file in a single call, and accepts a FILE_APPEND flag to add to the end of an existing file instead of overwriting it, similar to fopen()'s a mode.
Example: Simple Writes with file_put_contents()
<?php
// Call `file_put_contents("log.txt", "First entry\n")`
file_put_contents("log.txt", "First entry\n");
// Call `file_put_contents("log.txt", "Second entry\n", FILE_APPEND)`
file_put_contents("log.txt", "Second entry\n", FILE_APPEND);
// Print `file_get_contents("log.txt")` to the output
echo file_get_contents("log.txt");
?>
Login to try C/C++/Java/PHP code in the editor
Appending with file_put_contents()
Writing to a file the web server process doesn't have permission to modify fails silently or with a warning depending on your error settings, which is why checking fwrite()'s return value matters in production code.
Example: Appending with file_put_contents()
<?php
// Declare `$result`, set to `@file_put_contents("/root/protected.txt", "data")`
$result = @file_put_contents("/root/protected.txt", "data");
// Check whether `$result === false`
if ($result === false) {
// Print "Write failed -- check permissions" to the output
echo "Write failed -- check permissions";
}
?>
Login to try C/C++/Java/PHP code in the editor
Safe File Writing
For structured data like configuration or exported results, it's often better to encode it as JSON with json_encode() before writing, rather than hand-formatting plain text that's harder to read back reliably later.
Example: Safe File Writing
<?php
// Declare `$config` as an array: `["theme" => "dark", "lang" => "en"]`
$config = ["theme" => "dark", "lang" => "en"];
// Call `file_put_contents("config.json", json_encode($config))`
file_put_contents("config.json", json_encode($config));
// Print `file_get_contents("config.json")` to the output
echo file_get_contents("config.json");
?>
Login to try C/C++/Java/PHP code in the editor
- Using
wmode on an existing file and losing its old content; useato append. - Forgetting to pass
FILE_APPENDtofile_put_contents, which then overwrites the file. - Writing to a folder without write permission, so the write fails.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: