Skip to content
AgileLogics
4 min read

File I/O

Prerequisites: Working with JSON

file_put_contents('notes.txt', "Hello, file!\n");
$content = file_get_contents('notes.txt');
echo $content;

foreach (file('notes.txt') as $line) {
    echo $line;
}

if (file_exists('notes.txt')) {
    unlink('notes.txt'); // delete it
}

Common mistakes

  • Not checking a file operation's return value — file_get_contents returns false (not an exception) on failure, which silently becomes a confusing bug if used as if it were a real string.