IMPRESS dotted_line CONTACT dotted_line search dotted_line Kontakt dotted_line sitemap dotted_line print
148 PHP-Resource users online

Switch to another languags Deutsch aktuelle Sprache Englisch

PHP Manual


(PHP 4, PHP 5)

filepermsGets file permissions


int fileperms ( string $filename )

Gets permissions for the given file.



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 Mac OS X.

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.


Example #1 Display permissions as an octal value

echo substr(sprintf('%o'fileperms('/tmp')), -4);
substr(sprintf('%o'fileperms('/etc/passwd')), -4);

The above example will output:


Example #2 Display full permissions


if ((
$perms 0xC000) == 0xC000) {
// Socket
$info 's';
} elseif ((
$perms 0xA000) == 0xA000) {
// Symbolic Link
$info 'l';
} elseif ((
$perms 0x8000) == 0x8000) {
// Regular
$info '-';
} elseif ((
$perms 0x6000) == 0x6000) {
// Block special
$info 'b';
} elseif ((
$perms 0x4000) == 0x4000) {
// Directory
$info 'd';
} elseif ((
$perms 0x2000) == 0x2000) {
// Character special
$info 'c';
} elseif ((
$perms 0x1000) == 0x1000) {
// FIFO pipe
$info 'p';
} else {
// Unknown
$info 'u';

// Owner
$info .= (($perms 0x0100) ? 'r' '-');
$info .= (($perms 0x0080) ? 'w' '-');
$info .= (($perms 0x0040) ?
$perms 0x0800) ? 's' 'x' ) :
$perms 0x0800) ? 'S' '-'));

// Group
$info .= (($perms 0x0020) ? 'r' '-');
$info .= (($perms 0x0010) ? 'w' '-');
$info .= (($perms 0x0008) ?
$perms 0x0400) ? 's' 'x' ) :
$perms 0x0400) ? 'S' '-'));

// World
$info .= (($perms 0x0004) ? 'r' '-');
$info .= (($perms 0x0002) ? 'w' '-');
$info .= (($perms 0x0001) ?
$perms 0x0200) ? 't' 'x' ) :
$perms 0x0200) ? 'T' '-'));


The above example will output:



Upon failure, an E_WARNING is emitted.


Note: The results of this function are cached. See clearstatcache() for more details.


As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

See Also

Comments to the PHP manual
Write new comment


New Tutorial entries

Migration einer PHP 5 App auf PHP 7

Dieses PHP 7 Tutorial zeigt dir, wie du dein PHP5 Script auf PHP7 umstellst.

Berni | Category: PHP
PHP 7 Virtual Machine

Dieser Artikel zielt darauf ab, einen Überblick über die Zend Virtual Machine, wie es in PHP 7 gefunden wird.

Berni | Category: PHP
plotting masters - a professional guide - Teil II

Grafische Interpolation und Bestapproximation von numerischen Wertepaaren: Wir wollen Punkte auf einer Zeichenebene über verschiedene Verfahren miteinander verbinden.

EVAMasters | Category: PHP