Skip to content
Justice Tech School

Reading and Writing Files

file_put_contents and file_get_contents are the quick way. fopen gives streams.

Example
$f = sys_get_temp_dir() . "/t.txt";
file_put_contents($f, "a\nb\n");
echo count(file($f)), " ", trim(file_get_contents($f)), "\n";
unlink($f);

Result

2 a
b

Exercise

Write a file.

($f, "x")

Quiz Test

What does file() return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.