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<br/>    $filename = 'test.txt';<br/>    $perms = fileperms($filename);<br/>    echo "文件{$filename}的权限是:{$perms}";<br/>?>

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<br/>function format_perms($perms) {<br/>    $result = '';<br/>    if (($perms & 0xC000) == 0xC000) { $result .= 's'; }<br/>    elseif (($perms & 0xA000) == 0xA000) { $result .= 'l'; }<br/>    elseif (($perms & 0x8000) == 0x8000) { $result .= '-'; }<br/>    elseif (($perms & 0x6000) == 0x6000) { $result .= 'b'; }<br/>    elseif (($perms & 0x4000) == 0x4000) { $result .= 'd'; }<br/>    elseif (($perms & 0x2000) == 0x2000) { $result .= 'c'; }<br/>    elseif (($perms & 0x1000) == 0x1000) { $result .= 'p'; }<br/>    else { $result .= 'u'; }<br/>    // Owner permissions<br/>    $result .= ($perms & 0x0100) ? 'r' : '-';<br/>    $result .= ($perms & 0x0080) ? 'w' : '-';<br/>    if ($perms & 0x0040) { $result .= ($perms & 0x0800) ? 's' : 'x'; }<br/>    else { $result .= ($perms & 0x0800) ? 'S' : '-'; }<br/>    // Group permissions<br/>    $result .= ($perms & 0x0020) ? 'r' : '-';<br/>    $result .= ($perms & 0x0010) ? 'w' : '-';<br/>    if ($perms & 0x0008) { $result .= ($perms & 0x0400) ? 't' : 'x'; }<br/>    else { $result .= ($perms & 0x0400) ? 'T' : '-'; }<br/>    // Others permissions<br/>    $result .= ($perms & 0x0004) ? 'r' : '-';<br/>    $result .= ($perms & 0x0002) ? 'w' : '-';<br/>    $result .= ($perms & 0x0001) ? 'x' : '-';<br/>    return $result;<br/>}<br/>$filename = 'test.txt';<br/>$perms = fileperms($filename);<br/>$formatted_perms = format_perms($perms);<br/>echo "文件{$filename}的权限是:{$formatted_perms}";<br/>?>

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

php-functionsfile-permissionsfilepermscode-example
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

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.