fileperms
(PHP 4, PHP 5, PHP 7, PHP 8)
Gets file permissions
Description
Gets permissions for the given file.
Parameters
filenamePath to the file.
Return Values
Returns the file's permissions as a numeric mode. Lower bits of this mode are the same as the permissions expected by chmod(), however on most platforms the return value will also include information on the type of file given as filename. The examples below demonstrate how to test the return value for specific permissions and file types on POSIX systems, including Linux and macOS.
For local files, the specific return value is that of the st_mode member of the structure returned by the C library's stat() function. Exactly which bits are set can vary from platform to platform, and looking up your specific platform's documentation is recommended if parsing the non-permission bits of the return value is required.
Returns false on failure.
Errors/Exceptions Failure
Examples
Display permissions as an octal value
<?php
echo substr(sprintf('%o', fileperms('/tmp')), -4);
echo substr(sprintf('%o', fileperms('/etc/passwd')), -4);
?>The above example will output:
1777
0644Display full permissions
<?php
$perms = fileperms('/etc/passwd');
switch ($perms & 0xF000) {
case 0x1000: $info = 'p'; break; // FIFO pipe
case 0x2000: $info = 'c'; break; // character special
case 0x4000: $info = 'd'; break; // directory
case 0x6000: $info = 'b'; break; // block special
case 0x8000: $info = '-'; break; // regular
case 0xA000: $info = 'l'; break; // symbolic link (reachable when using lstat function)
case 0xC000: $info = 's'; break; // socket
// unknown
default: $info = 'u';
}
// Owner
$setuid = $perms & 0x0800;
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ? ($setuid ? 's' : 'x') : ($setuid ? 'S' : '-'));
// Group
$setgid = $perms & 0x0400;
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ? ($setgid ? 's' : 'x') : ($setgid ? 'S' : '-'));
// World
$sticky = $perms & 0x0200;
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ? ($sticky ? 't' : 'x') : ($sticky ? 'T' : '-'));
echo $info;
?>The above example will output:
-rw-r--r--