Skip to content
Justice Tech School

Files

file_put_contents writes and file_get_contents reads a whole file. fopen, fgets and fclose give line by line control. file_exists and unlink check and delete. Always check the result, because the file may not exist.

Example
$path = sys_get_temp_dir() . "/notes_demo.txt";
file_put_contents($path, "first\nsecond\n");
file_put_contents($path, "third\n", FILE_APPEND);
$lines = file($path, FILE_IGNORE_NEW_LINES);
print_r($lines);
echo strlen(file_get_contents($path)), "\n";
unlink($path);
var_dump(file_exists($path));

Result

Array
(
    [0] => first
    [1] => second
    [2] => third
)
19
bool(false)

Exercise

Read a whole file.

$text = ($path);

Quiz Test

Which function deletes a file?

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