Discussion:
question on stat function
Jeffry Killen
2014-03-12 20:48:41 UTC
Permalink
question on stat function
from Wikipedia entry for stat function (in c):
The st_mode field is a bit field. It combines the file access modes
and also indicates any special file type.
I believe that the entry for stat in php manual references this.

How would you interpret this to determine the files access perms?

(for the sake of custom error handling or whatever)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-03-12 21:53:52 UTC
Permalink
Post by Jeffry Killen
question on stat function
The st_mode field is a bit field. It combines the file access modes and also
indicates any special file type.
I believe that the entry for stat in php manual references this.
How would you interpret this to determine the files access perms?
(for the sake of custom error handling or whatever)
Their are a couple user contributed notes that describe how to decode this
value from the array output of stat().

I ripped this from one of them.

<?php

$f=stat('somefile.txt');

$ts=array(
0140000=>'ssocket',
0120000=>'llink',
0100000=>'-file',
0060000=>'bblock',
0040000=>'ddir',
0020000=>'cchar',
0010000=>'pfifo'
);

$m = $f['mode'];

$t=decoct($m & 0170000); // File Encoding Bit

$str = (array_key_exists(octdec($t),$ts))?$ts[octdec($t)]{0}:'u';
$str .= (($m&0x0100)?'r':'-');
$str .= (($m&0x0080)?'w':'-');
$str .= (($m&0x0040)?(($m&0x0800)?'s':'x'):(($m&0x0800)?'S':'-'));
$str .= (($m&0x0020)?'r':'-');
$str .= (($m&0x0010)?'w':'-');
$str .= (($m&0x0008)?(($m&0x0400)?'s':'x'):(($m&0x0400)?'S':'-'));
$str .= (($m&0x0004)?'r':'-');
$str .= (($m&0x0002)?'w':'-');
$str .= (($m&0x0001)?(($m&0x0200)?'t':'x'):(($m&0x0200)?'T':'-'));

echo $str;

YMMV
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-03-12 22:25:09 UTC
Permalink
Post by Jim Lucas
Post by Jeffry Killen
question on stat function
The st_mode field is a bit field. It combines the file access modes and also
indicates any special file type.
I believe that the entry for stat in php manual references this.
How would you interpret this to determine the files access perms?
(for the sake of custom error handling or whatever)
Their are a couple user contributed notes that describe how to decode this
value from the array output of stat().
I ripped this from one of them.
<?php
$f=stat('somefile.txt');
$ts=array(
0140000=>'ssocket',
0120000=>'llink',
0100000=>'-file',
0060000=>'bblock',
0040000=>'ddir',
0020000=>'cchar',
0010000=>'pfifo'
);
$m = $f['mode'];
$t=decoct($m & 0170000); // File Encoding Bit
$str = (array_key_exists(octdec($t),$ts))?$ts[octdec($t)]{0}:'u';
$str .= (($m&0x0100)?'r':'-');
$str .= (($m&0x0080)?'w':'-');
$str .= (($m&0x0040)?(($m&0x0800)?'s':'x'):(($m&0x0800)?'S':'-'));
$str .= (($m&0x0020)?'r':'-');
$str .= (($m&0x0010)?'w':'-');
$str .= (($m&0x0008)?(($m&0x0400)?'s':'x'):(($m&0x0400)?'S':'-'));
$str .= (($m&0x0004)?'r':'-');
$str .= (($m&0x0002)?'w':'-');
$str .= (($m&0x0001)?(($m&0x0200)?'t':'x'):(($m&0x0200)?'T':'-'));
echo $str;
YMMV
--
Jim Lucas
http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
If the user contributed logic is hard to understand, this example can
simplify it for you:
http://php.net/fileperms
(same logic different method)

Continue reading on narkive:
Loading...