Re: question on stat function
| From: | Aziz Saleh | Date: | Wed, 12 Mar 2014 22:25:09 +0000 |
| Subject: | Re: question on stat function | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323052@lists.php.net to get a copy of this message | ||
On Wed, Mar 12, 2014 at 5:53 PM, Jim Lucas <lists@cmsws.com> wrote:
> On 03/12/2014 01:48 PM, Jeffry Killen wrote:
>
>> 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)
>>
>
> 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)