If you only want the permissions (lowest three octal numbers) you can use a bitwise AND to mask the bits:
<?php
fileperms($file) & 511;
?>
fileperms
(PHP 4, PHP 5)
fileperms — Pobiera prawa dostępu pliku
Opis
int fileperms
( string $nazwa_pliku
)
Zwraca prawa dostępu pliku, lub FALSE w przypadku błędu.
Informacja: Wyniki działania tej funkcji są buforowane. Zobacz opis funkcji clearstatcache() aby uzyskać więcej informacji.
Wskazówka
Od PHP 5.0.0 ta funkcja może być użyta także z niektórymi nakładkami URL. Zobacz List of Supported Protocols/Wrappers aby uzyskać listę nakładek, które obsługują funkcjonalność z rodziny stat().
Example #1 Wyświetlanie uprawnień w postaci ósemkowej
<?php
echo substr(sprintf('%o', fileperms('/tmp')), -4);
echo substr(sprintf('%o', fileperms('/etc/passwd')), -4);
?>
To powino wyświetlić:
1777 0644
Example #2 Wyświetlanie wszystkich uprawnień
<?php
$perms = fileperms('/etc/passwd');
if (($perms & 0xC000) == 0xC000) {
// Gniazdo (socket)
$info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
// Link symboliczny
$info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
// Zwykły plik
$info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
// Urządzenie blokowe
$info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
// Katalog
$info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
// Urządzenie znakowe
$info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
// Potok (FIFO)
$info = 'p';
} else {
// Nieznane
$info = 'u';
}
// Właściciel
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
// Grupa
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
// Świat
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
echo $info;
?>
To powino wyświetlić:
-rw-r--r--
Patrz także: is_readable(), i stat()
fileperms
eelco
10-Jul-2007 11:21
10-Jul-2007 11:21
paul2712 at gmail dot com
02-Jun-2007 06:08
02-Jun-2007 06:08
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);
chinello at gmail dot com
25-Apr-2007 06:43
25-Apr-2007 06:43
On Linux (not tested on Windows), if you want a chmod-like permissions, you can use this function:
<?php
function file_perms($file, $octal = false)
{
if(!file_exists($file)) return false;
$perms = fileperms($file);
$cut = $octal ? 2 : 3;
return substr(decoct($perms), $cut);
}
?>
Using it:
$ touch foo.bar
$ chmod 0754 foo.bar
<?php
echo file_perms('foo.bar'); // prints: 754
echo file_perms('foo.bar', true); // prints 0754
?>
