is_readable
Checks that a file exists and can be read.
Syntax
is_readable(string $filename): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $filename | string | The path to the file. |
Return value
bool`true` if the file can be read.
Examples
Checking before reading:
<?php
$file = sys_get_temp_dir() . '/ref-readable.txt';
file_put_contents($file, 'data');
echo is_readable($file) ? file_get_contents($file) : 'cannot read', "\n";
var_dump(is_readable(sys_get_temp_dir() . '/no-such-file.txt'));Output:
data
bool(false)Notes
Without the check file_get_contents() emits a warning and returns false for an unreadable file.
See also
- is_file — Checks that a path exists and is a file rather than a folder.
- is_writable — Checks whether a file or folder can be written to.
- file_get_contents — Reads a whole file into a string.
Related recipes
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.