File Basics
Write a file
file_put_contents is a quick way to save text.
<?php
$path = __DIR__ . "/notes.txt";
file_put_contents($path, "Hello from PHP\n");
Read a file
Confirm the file exists before reading.
if (is_file($path)) {
echo file_get_contents($path);
}
Append lines
Use flags when you want to add without wiping prior content.
file_put_contents($path, "Another line\n", FILE_APPEND);
File safety
- Never let users choose arbitrary paths unchecked
- Prefer paths under your project directory
- Check return values and handle failure
- For large files, use streams instead of loading everything
Comments
One comment per signed-in account. Comments are saved with this page’s URL.