Re: unix permission converter

From: Date: Tue, 21 Aug 2001 23:06:59 +0000
Subject: Re: unix permission converter
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-63809@lists.php.net to get a copy of this message
> I have written a "Permission Converter" which converts unix permissions (the > ones we know from CHMOD) into a "-" delimited string of numbers. I also made > it that it supports one more level (unix only has 3 different types of > permissions, (r,w,x) while mine can handle a fourth one. Since I'm just a > newbie to PHP, I was wondering whether there is a shorter way of achieving > the same thing. Here is my code: "-" is probably not the best choice, since most Un*x boxes use "-" to display "None" of rxw set in "ls" and other human-readable output. You would probably win more fans if you just use the same format as "ls" Also, what's the fourth level for?... If the OS doesn't support it, I'm having a hard time figuring out what possible good it does... > // $num is the number (range: 1-15) > $done = ""; > $result = ""; > > while ($done != 1) { > > if ($num > 8) { > $num = $num - 8; > $result = "8-"; > } elseif ($num == 8) { > $result = 8; > $done = 1; > } elseif ($num > 4) { > $num = $num - 4; > $result .= "4-"; > } elseif ($num == 4) { > $result .= 4; > $done = 1; > } elseif ($num > 2) { > $result .= "2-1"; > $done = 1; > } elseif ($num == 2) { > $result .= "2-"; > $done = 1; > } else { > $result .= "1"; > $done = 1; > } > > } You probably should be looking at >> and << (bit-shift operators)... This function can probably be reduced to three lines of code, maybe even one. > if ((strrpos($result, "-") == strlen($result)-1) && strlen($result) > 1) { > $result = strrev($result); > $result = substr($result, 1); > $result = strrev($result); > } > > echo $result; Finally... *WHY* are you doing this?... I know the whole rwx and octal/hex notation can be tricky at first, but there's a reason why it's been done that way for quite a *LONG* time... -- WARNING richard@zend.com address is an endangered species -- Use ceo@l-i-e.com Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm

« previous php.general (#63809) next »