Backend Development 5 min read

Using PHP fileperms() to Retrieve and Format File Permissions

This article explains how the PHP fileperms() function can be used to obtain a file's permission bits, demonstrates a basic usage example, and provides a helper function that converts the numeric result into a human‑readable permission string such as -rw‑r--r--.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP fileperms() to Retrieve and Format File Permissions

In PHP development, the fileperms() function is used to retrieve a file's permission information, allowing you to determine whether the file is readable, writable, or executable.

The function signature is string fileperms ( string $filename ) , where $filename is the path to the target file and the return value is a string representing the file's permission bits.

A simple example shows how to call fileperms() on a file named test.txt and echo the raw permission value:

<?php
$filename = 'test.txt';
$perms = fileperms($filename);
echo "文件{$filename}的权限是:{$perms}";
?>

Running this script typically outputs an integer such as 33204 , which encodes the read, write, and execute flags in its lower nine bits.

To translate the numeric result into the familiar symbolic format (e.g., -rw-r--r-- ), a helper function format_perms() is provided. It examines each permission bit using bitwise operations and builds a string that reflects file type, owner, group, and others permissions, including special bits like setuid, setgid, and sticky.

<?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'; }
// Owner permissions
$result .= ($perms & 0x0100) ? 'r' : '-';
$result .= ($perms & 0x0080) ? 'w' : '-';
if ($perms & 0x0040) { $result .= ($perms & 0x0800) ? 's' : 'x'; }
else { $result .= ($perms & 0x0800) ? 'S' : '-'; }
// Group permissions
$result .= ($perms & 0x0020) ? 'r' : '-';
$result .= ($perms & 0x0010) ? 'w' : '-';
if ($perms & 0x0008) { $result .= ($perms & 0x0400) ? 't' : 'x'; }
else { $result .= ($perms & 0x0400) ? 'T' : '-'; }
// Others permissions
$result .= ($perms & 0x0004) ? 'r' : '-';
$result .= ($perms & 0x0002) ? 'w' : '-';
$result .= ($perms & 0x0001) ? 'x' : '-';
return $result;
}
$filename = 'test.txt';
$perms = fileperms($filename);
$formatted_perms = format_perms($perms);
echo "文件{$filename}的权限是:{$formatted_perms}";
?>

Executing the full script produces output such as 文件test.txt的权限是:-rw-r--r-- , demonstrating that the helper function successfully converts the raw permission integer into a readable string.

Overall, by leveraging fileperms() together with a custom formatter, developers can programmatically inspect and display file permissions in PHP applications.

BackendPHPCode ExampleFile Permissionsphp-functionsfileperms
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.