Mastering PHP fileperms(): Retrieve and Interpret File Permissions

Learn how to use PHP's fileperms() function to obtain file permission data, interpret the returned integer with bitwise operations, and format it into a readable string using a helper function, complete with practical code examples and detailed explanations.

php Courses
php Courses
php Courses
Mastering PHP fileperms(): Retrieve and Interpret File Permissions

In PHP development, you may need to obtain a file's permission information, such as checking whether it is readable, writable, or executable. The PHP function fileperms() helps achieve this. This article introduces the usage and examples of fileperms().

The fileperms() function retrieves a file's permissions. Its prototype is: string fileperms ( string $filename ) Here $filename is the path of the file whose permissions you want to get. The function returns a string representing the file's permissions.

Example of using fileperms() to get a file's permissions:

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

Running this code might output something like: 文件test.txt的权限是:33204 The returned integer encodes the permission bits; the lower nine bits indicate read, write, and execute permissions. To convert this integer into a human‑readable permission string, you can use a helper function:

<?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'; }

    if (($perms & 0x0100) == 0x0100) { $result .= 'r'; } else { $result .= '-'; }
    if (($perms & 0x0080) == 0x0080) { $result .= 'w'; } else { $result .= '-'; }

    if (($perms & 0x0040) == 0x0040) {
        if ($perms & 0x0800) { $result .= 's'; } else { $result .= 'x'; }
    } else {
        if ($perms & 0x0800) { $result .= 'S'; } else { $result .= '-'; }
    }

    if (($perms & 0x0008) == 0x0008) {
        if ($perms & 0x0400) { $result .= 't'; } else { $result .= 'x'; }
    } else {
        if ($perms & 0x0400) { $result .= 'T'; } else { $result .= '-'; }
    }

    if (($perms & 0x0004) == 0x0004) { $result .= 'r'; } else { $result .= '-'; }
    if (($perms & 0x0002) == 0x0002) { $result .= 'w'; } else { $result .= '-'; }
    if (($perms & 0x0001) == 0x0001) { $result .= 'x'; } else { $result .= '-'; }

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

Running this code may produce an output such as:

文件test.txt的权限是:-rw-r--r--

Summary

In PHP development, the fileperms() function allows you to easily retrieve a file's permission bits; by applying bitwise operations or a helper format_perms() function, you can convert the integer into a readable permission string, ensuring your file operations respect the required permissions.

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.

PHPbitwise operationsFile Permissionsfileperms
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.