PHP: Hypertext Preprocessor (original) (raw)
fileperms
(PHP 4, PHP 5, PHP 7, PHP 8)
fileperms — Gets file permissions
Description
Parameters
filename
Path 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 thest_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](reserved.constants.php#constant.false)
on failure.
Errors/Exceptions
Upon failure, an [E_WARNING](errorfunc.constants.php#constant.e-warning)
is emitted.
Examples
Example #1 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:
Example #2 Display full permissions
`<?php
$perms = fileperms('/etc/passwd');
switch (
$perms & 0xF000) {
case 0xC000: // socket
$info = 's';
break;
case 0xA000: // symbolic link
$info = 'l';
break;
case 0x8000: // regular
$info = 'r';
break;
case 0x6000: // block special
$info = 'b';
break;
case 0x4000: // directory
$info = 'd';
break;
case 0x2000: // character special
$info = 'c';
break;
case 0x1000: // FIFO pipe
$info = 'p';
break;
default: // unknown
$info = 'u';
}// Owner info.=((info .= ((info.=((perms & 0x0100) ? 'r' : '-'); info.=((info .= ((info.=((perms & 0x0080) ? 'w' : '-'); info.=((info .= ((info.=((perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));// Group info.=((info .= ((info.=((perms & 0x0020) ? 'r' : '-'); info.=((info .= ((info.=((perms & 0x0010) ? 'w' : '-'); info.=((info .= ((info.=((perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));// World info.=((info .= ((info.=((perms & 0x0004) ? 'r' : '-'); info.=((info .= ((info.=((perms & 0x0002) ? 'w' : '-'); info.=((info .= ((info.=((perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
echo
$info;
?>`
The above example will output:
Notes
Note: The results of this function are cached. See clearstatcache() for more details.
See Also
- chmod() - Changes file mode
- is_readable() - Tells whether a file exists and is readable
- stat() - Gives information about a file
Found A Problem?
11 years ago
`Don't use substr, use bit operator
If you want to compare permission
`
13 years ago
`This may not be immediately apparent to some, but you can use octdec( $octal_value ) to match the permissions retrieved by file perms
bit="102770";printf("bit = "102770";printf( "%s\n", octdec( bit="102770";printf("bit ) ); printf( "%s\n", $perm);?>`
17 years ago
`Do not forget: clearstatcache();
When ever you make a:
mkdir($dstdir, 0770 ))
or a:
chmod($dstdir, 0774 );
You have to call:
clearstatcache();
before you can call:
fileperms($dstdir);
`
5 years ago
`Windows has a very different file permission model to Unix and integrates them only minimally.
Here's how Windows calculates the bitmask...
u+w/g+w/o+w is set based on whether the file has the read only flag.
u+r/g+w/o+w is always set.
u+x/g+x/o+x is set based on whether $filename is an inherently executable file (e.g. bat) or a directory.
Windows isn't integrating its ACLs at all.
Here's the source of all this: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2019 (but it doesn't provide many details)
`