Example 1 — Check Before Reading a File
Create a file and read it only when is_readable() says it is safe.
<?php
$file = sys_get_temp_dir() . '/is_readable_demo.txt';
file_put_contents($file, 'Hello CodeToFun');
if (is_readable($file)) {
echo file_get_contents($file), PHP_EOL;
}
unlink($file); How It Works
- file_put_contents() creates a file that PHP itself can read.
- is_readable() returns true, so the if block runs.
- file_get_contents() reads the content and echo prints it.
- unlink() removes the temporary file.

