How to Retrieve and Interpret File Permissions in PHP with fileperms()
This guide explains how PHP's fileperms() function retrieves a file's permission bits, demonstrates converting the returned integer into a human‑readable string, and provides sample code for displaying permissions such as rw‑r‑‑r‑‑ for typical Unix files.
In PHP development you often need to know whether a file is readable, writable or executable. The built‑in fileperms() function returns the permission bits of a given file path as an integer.
Function prototype
string fileperms ( string $filename )The $filename argument is the path to the file whose permissions you want to query. The function returns an integer that encodes the file mode.
Simple usage example
<?php
$filename = 'test.txt';
$perms = fileperms($filename);
echo "File {$filename} permissions: {$perms}";
?>Running this script might output something like 33204. The integer represents the permission bits; the lower nine bits correspond to the classic Unix read/write/execute flags for owner, group, and others.
Understanding the bits
Bit 0 (0x0001) – execute for others
Bit 1 (0x0002) – write for others
Bit 2 (0x0004) – read for others
Bit 3 (0x0008) – execute for group (with set‑gid handling)
Bit 4 (0x0010) – write for group
Bit 5 (0x0020) – read for group
Bit 6 (0x0040) – execute for owner (with set‑uid handling)
Bit 7 (0x0080) – write for owner
Bit 8 (0x0100) – read for owner
Because the integer mixes file type and permission bits, you typically need to mask and shift the value to extract a readable string.
Helper function to format permissions
<?php
function format_perms($perms) {
$result = '';
if (($perms & 0xC000) == 0xC000) { $result .= 's'; }
elseif (($perms & 0xA000) == 0xA000) { $result .= 'l'; }
elseif (($perms & 0x8000) == 0x8000) { $result .= '-'; }
elseif (($perms & 0x6000) == 0x6000) { $result .= 'b'; }
elseif (($perms & 0x4000) == 0x4000) { $result .= 'd'; }
elseif (($perms & 0x2000) == 0x2000) { $result .= 'c'; }
elseif (($perms & 0x1000) == 0x1000) { $result .= 'p'; }
else { $result .= 'u'; }
$result .= ($perms & 0x0100) ? 'r' : '-';
$result .= ($perms & 0x0080) ? 'w' : '-';
if ($perms & 0x0040) {
$result .= ($perms & 0x0800) ? 's' : 'x';
} else {
$result .= ($perms & 0x0800) ? 'S' : '-';
}
$result .= ($perms & 0x0020) ? 'r' : '-';
$result .= ($perms & 0x0010) ? 'w' : '-';
if ($perms & 0x0008) {
$result .= ($perms & 0x0400) ? 't' : 'x';
} else {
$result .= ($perms & 0x0400) ? 'T' : '-';
}
$result .= ($perms & 0x0004) ? 'r' : '-';
$result .= ($perms & 0x0002) ? 'w' : '-';
$result .= ($perms & 0x0001) ? 'x' : '-';
return $result;
}
?>This function examines the mode bits and builds a string similar to the output of ls -l, e.g., -rw-r--r--.
Full example with formatted output
<?php
$filename = 'test.txt';
$perms = fileperms($filename);
$formatted = format_perms($perms);
echo "File {$filename} permissions: {$formatted}";
?>When executed, the script might display File test.txt permissions: -rw-r--r--, showing that the owner can read and write, while the group and others can only read.
Conclusion
Using fileperms() together with a small formatter lets PHP developers quickly inspect file permission settings, ensuring that file operations respect the intended access controls.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
