Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<?php // Basic file read/write example $path = __DIR__ . '/demo.txt'; // Write (overwrite) file_put_contents($path, "Hello, file!\n"); // Append another line file_put_contents($path, "Second line\n", FILE_APPEND | LOCK_EX); // Check before reading if (file_exists($path) && is_readable($path)) { echo file_get_contents($path); } else { echo 'File not found.'; } // Read line-by-line into an array $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { echo $line . "<br>"; } ?>
Output