is_readable
is_readable(string $filename): bool
Whether a file/directory exists and is readable (real UID/GID check, stat-cached); the family also has is_writable/is_executable.
C C23
#include <stdio.h>
#include <unistd.h>
int main(void) {
/* access(R_OK) checks readability using the REAL uid, not the effective
one - the same caveat PHP's is_readable manual documents (SUID care) */
const char *path = "/tmp/a18read_c.txt";
FILE *fp = fopen(path, "w");
fputs("data", fp);
fclose(fp);
printf("%d\n", access(path, R_OK) == 0);
printf("%d\n", access("/tmp/a18read_c.nonexistent", R_OK) == 0);
unlink(path);
return 0;
}
1 0
C++ C++26
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
int main() {
// C++ has no access(2): the portable idiom is to try opening with ifstream
const std::string path =
(std::filesystem::temp_directory_path() / "a18read_cpp.txt").string();
{
std::ofstream out(path);
out << "data";
}
std::ifstream in(path);
std::cout << std::boolalpha << in.is_open() << "\n";
in.close();
std::ifstream missing(path + ".nonexistent");
std::cout << missing.is_open() << "\n";
std::remove(path.c_str());
return 0;
}
true false
C# v11.0.100
// FileInfo.Attributes: Windows models writability as a read-only flag, so a
// plain "is readable" check reduces to existence (access-denied needs try/open)
string path = Path.Combine(Path.GetTempPath(), "a18read_cs.txt");
File.WriteAllText(path, "data");
var info = new FileInfo(path);
Console.WriteLine(info.Exists);
Console.WriteLine(info.Attributes.HasFlag(FileAttributes.ReadOnly));
Console.WriteLine(new FileInfo(path + ".nonexistent").Exists);
File.Delete(path);
True False False
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
// Go stdlib has no access(2): the idiom is to try opening for real
f, err := os.CreateTemp("", "a18read")
if err != nil {
panic(err)
}
name := f.Name()
f.Close()
fh, err := os.Open(name)
if err != nil {
fmt.Println("not readable:", err)
} else {
fmt.Println("readable")
fh.Close()
}
_, err = os.Open(name + ".nonexistent")
fmt.Println(err != nil)
}
readable true
Java v27.0.1
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
// File.canRead() checks readability (JVM maps it to access(R_OK))
Path p = Files.createTempFile("a18read", ".txt");
System.out.println(p.toFile().canRead());
System.out.println(p.resolveSibling(p.getFileName() + ".nonexistent").toFile().canRead());
Files.delete(p);
}
}
true false
JavaScript v26.13.1
// fs.accessSync(path, R_OK): returns undefined on pass, throws on failure
const fs = require("fs");
const path = require("os").tmpdir() + "/a18_readable.txt";
fs.writeFileSync(path, "data");
console.log(fs.accessSync(path, fs.constants.R_OK) === undefined);
try {
fs.accessSync(path + ".nonexistent", fs.constants.R_OK);
console.log("readable");
} catch {
console.log(false);
}
fs.unlinkSync(path);
// Promise form: fs.promises.access resolves on pass, rejects on failure
fs.promises.access(path + ".nonexistent", fs.constants.R_OK).then(
() => console.log("readable"),
() => console.log("rejected")
);
true false rejected
PHP v8.6.0
// is_readable checks existence + readability (real UID/GID, per the manual)
$f = tempnam(sys_get_temp_dir(), 'rd');
var_dump(is_readable($f));
var_dump(is_readable($f . '.nonexistent'));
// Directories report true as well - disambiguate with is_dir
var_dump(is_readable(sys_get_temp_dir()));
unlink($f);
bool(true) bool(false) bool(true)
Python v3.15.2
import os
import tempfile
# os.access(R_OK): readability check (real uid semantics, like C access(2))
path = os.path.join(tempfile.gettempdir(), "a18_readable.txt")
with open(path, "w") as fh:
fh.write("data")
print(os.access(path, os.R_OK))
print(os.access(path + ".nonexistent", os.R_OK))
print(os.access(tempfile.gettempdir(), os.R_OK))
os.remove(path)
True False True
Rust v1.99.1
fn main() {
// metadata().permissions() exposes mode bits: check the POSIX read bits
// (Unix-only; Windows permissions need a different model)
use std::os::unix::fs::PermissionsExt;
let path = std::env::temp_dir().join("a18read_rs.txt");
std::fs::write(&path, "data").unwrap();
let mode = std::fs::metadata(&path).unwrap().permissions().mode();
println!("{}", mode & 0o444 != 0);
println!("{}", std::fs::metadata(path.with_extension("nonexistent")).is_err());
std::fs::remove_file(&path).unwrap();
}
true true
TypeScript v26.13.1
// fs.accessSync(path, R_OK): returns undefined on pass, throws on failure
import * as fs from "fs";
import * as os from "os";
const path = os.tmpdir() + "/a18_readable.txt";
fs.writeFileSync(path, "data");
console.log(fs.accessSync(path, fs.constants.R_OK) === undefined);
try {
fs.accessSync(path + ".nonexistent", fs.constants.R_OK);
console.log("readable");
} catch {
console.log(false);
}
fs.unlinkSync(path);
fs.promises.access(path + ".nonexistent", fs.constants.R_OK).then(
() => console.log("readable"),
() => console.log("rejected")
);
true false rejected
Language differences
C: access(path, R_OK) - checks with the REAL uid, not the effective one (manual security point: SUID programs using access to ask "can I read now" misjudge - a TOCTOU-bug breeding ground; prefer a real open with error handling); the family has W_OK/X_OK/F_OK.
C++: no access(2) binding - the portable idiom is an ifstream try-open with is_open(); C++17 filesystem has permissions()/status() but "can the current user read" still needs a try-open.
C#: FileInfo.Attributes only carries a read-only flag (Windows semantics: readability modeled as "not writable") - a plain "is readable" check degrades to existence; access-denied needs a try-open; on Unix .NET maps mode bits but the semantics stay simplified.
Go: the stdlib has no access(2) - the idiom is a real os.Open try-open (the error branch tells you why it is unreadable); golang.org/x/sys/unix.Access offers a true access (external dependency).
Java: File.canRead() - the JVM maps it to the OS's access(R_OK); Files.isReadable(Path) is the NIO variant; Java exposes no real-vs-effective UID distinction - the running identity is the JVM process identity.
JavaScript: fs.accessSync(path, fs.constants.R_OK) returns undefined on pass and throws on failure (exception-style semantics, the opposite of PHP's boolean style); the Promise form fs.promises.access resolves/rejects likewise; the family has W_OK/X_OK/F_OK.
PHP: is_readable checks with the real UID/GID (manual-stated - same semantics as C access, mind SUID/setuid CLI setups); directories report true too (disambiguate with is_dir); results are subject to the stat cache (clearstatcache); the family has is_writable/is_executable; on the web PHP usually accesses files as the web-server user (e.g. nobody), which the manual notes is the effective identity of the check.
Python: os.access(path, os.R_OK) - real-uid semantics like C; os.path.isfile/os.stat combine well; note os.access is always true for root (root reads everything - the same pit as PHP/JS).
Rust: metadata().permissions() exposes POSIX mode bits (PermissionsExt::mode()) - "can the current user read" needs uid/bit arithmetic yourself; cross-platform code needs #[cfg(unix)] (Windows permissions are a different model).
TypeScript: same as JavaScript (accessSync/promises.access + R_OK constants).