fileperms

Gets file permissions

Syntax

fileperms ( string $filename ) : int

Parameters

filename

Path to the file.

Return

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.

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.

Examples

1

<?

$filename = "/etc/passwd";

$return = fileperms($filename);

echo $return;

?>
33188

2

<?

$filename = "/etc/passwd";

$return = fileperms($filename);

echo substr(sprintf("%o", $return), -4);

?>
0644

3

<?

$filename = "/etc/passwd";

$return = fileperms($filename);

switch ($return & 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 .= (($return & 0x0100) ? "r" : "-");
$info .= (($return & 0x0080) ? "w" : "-");
$info .= (($return & 0x0040) ?
    (($return & 0x0800) ? "s" : "x") :
    (($return & 0x0800) ? "S" : "-"));

// group
$info .= (($return & 0x0020) ? "r" : "-");
$info .= (($return & 0x0010) ? "w" : "-");
$info .= (($return & 0x0008) ?
    (($return & 0x0400) ? "s" : "x") :
    (($return & 0x0400) ? "S" : "-"));

// world
$info .= (($return & 0x0004) ? "r" : "-");
$info .= (($return & 0x0002) ? "w" : "-");
$info .= (($return & 0x0001) ?
    (($return & 0x0200) ? "t" : "x") :
    (($return & 0x0200) ? "T" : "-"));

echo $info;

?>
rrw-r--r--

basename

chgrp

chmod

chown

clearstatcache

copy

dirname

disk_free_space

disk_total_space

diskfreespace

fclose

feof

fflush

fgetc

fgetcsv

fgets

file

file_exists

file_get_contents

file_put_contents

fileatime

filectime

filegroup

fileinode

filemtime

fileowner

filesize

filetype

flock

fnmatch

fopen

fpassthru

fputcsv

fputs

fread

fscanf

fseek

fstat

ftell

ftruncate

fwrite

glob

is_dir

is_executable

is_file

is_link

is_readable

is_uploaded_file

is_writable

is_writeable

lchgrp

lchown

link

linkinfo

lstat

mkdir

move_uploaded_file

pathinfo

pclose

popen

readfile

readlink

realpath

realpath_cache_get

realpath_cache_size

rename

rewind

rmdir

set_file_buffer

stat

symlink

tempnam

tmpfile

touch

umask

unlink

PHP

Home Menu