note 92144 added to function.chmod
| From: | MeaulnesLegler at osu1 dot php dot net | Date: | Fri, 10 Jul 2009 14:38:34 +0000 |
| Subject: | note 92144 added to function.chmod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158170@lists.php.net to get a copy of this message | ||
Another converter from 'rwx' mode to octal, but considering the setuid/setgid and sticky
bits, too. Also the reverse function, converts from octal to 'rwx' mode. Both ignore the
leading 'd' or '-' since no directory nor file information is provided.
<?php
function ModeRWX2Octal($Mode_rwx) { // enter rwx mode, e.g. 'drwxr-sr-x'
if ( ! preg_match("/[-d]?([-r][-w][-xsS]){2}[-r][-w][-xtT]/", $Mode_rwx) )
die("wrong <TT>-rwx</TT> mode in
ModeRWX2Octal('<TT>$Mode_rwx</TT>')");
$Mrwx = substr($Mode_rwx, -9); // 9 chars from the right-hand side
$ModeDecStr = (preg_match("/[sS]/",$Mrwx[2]))?4:0; // pick out sticky
$ModeDecStr .= (preg_match("/[sS]/",$Mrwx[5]))?2:0; // _ bits and change
$ModeDecStr .= (preg_match("/[tT]/",$Mrwx[8]))?1:0; // _ to e.g. '020'
$Moctal = $ModeDecStr[0]+$ModeDecStr[1]+$ModeDecStr[2]; // add them
$Mrwx = str_replace(array('s','t'), "x", $Mrwx); // change execute
bit
$Mrwx = str_replace(array('S','T'), "-", $Mrwx); // _ to on or off
$trans =
array('-'=>'0','r'=>'4','w'=>'2','x'=>'1'); //
prepare for strtr
$ModeDecStr .= strtr($Mrwx,$trans); // translate to e.g. '020421401401'
$Moctal .= $ModeDecStr[3]+$ModeDecStr[4]+$ModeDecStr[5]; // continue
$Moctal .= $ModeDecStr[6]+$ModeDecStr[7]+$ModeDecStr[8]; // _ adding
$Moctal .= $ModeDecStr[9]+$ModeDecStr[10]+$ModeDecStr[11]; // _ triplets
return $Moctal; // returns octal mode, e.g. '2755' from above.
}
function ModeOctal2rwx($ModeOctal) { // enter octal mode, e.g. '644' or '2755'
if ( ! preg_match("/[0-7]{3,4}/", $ModeOctal) ) // either 3 or 4 digits
die("wrong octal mode in ModeOctal2rwx('<TT>$ModeOctal</TT>')");
$Moctal = ((strlen($ModeOctal)==3)?"0":"").$ModeOctal; // assume default 0
$Mode3 = substr($Moctal,-3); // trailing 3 digits, no sticky bits considered
$RWX = array
('---','--x','-w-','-wx','r--','r-x','rw-','rwx'); //
dumb,huh?
$Mrwx = $RWX[$Mode3[0]].$RWX[$Mode3[1]].$RWX[$Mode3[2]]; // concatenate
if (preg_match("/[1357]/", $Moctal[0])) $Mrwx[8] =
($Mrwx[8]=="-")?"T":"t";
if (preg_match("/[2367]/", $Moctal[0])) $Mrwx[5] =
($Mrwx[5]=="-")?"S":"s";
if (preg_match("/[4567]/", $Moctal[0])) $Mrwx[2] =
($Mrwx[2]=="-")?"S":"s";
return $Mrwx; // returns e.g. 'rw-r--r--' or 'rwxr-sr-x'
}
?>
following test code returns the results below
<?php
$Mode = "-rw-rw-r--";
echo "<BR>ModeRWX2Octal(<TT>$Mode) -></TT>
".ModeRWX2Octal($Mode)."\n";
$Mode = "drwxr-sr-x";
echo "<BR>ModeRWX2Octal(<TT>$Mode) -></TT>
".ModeRWX2Octal($Mode)."\n";
$Mode = "644";
echo "<BR>ModeOctal2rwx($Mode) <TT>->
".ModeOctal2rwx($Mode)."</TT>\n";
$Mode = "2755";
echo "<BR>ModeOctal2rwx($Mode) <TT>->
".ModeOctal2rwx($Mode)."</TT>\n";
?>
ModeRWX2Octal(-rw-rw-r--) -> 0664
ModeRWX2Octal(drwxr-sr-x) -> 2755
ModeOctal2rwx(644) -> rw-r--r--
ModeOctal2rwx(2755) -> rwxr-sr-x
----
Server IP: 62.2.182.223
Probable Submitter: 144.85.145.249
----
Manual Page -- http://www.php.net/manual/en/function.chmod.php
Edit -- https://master.php.net/note/edit/92144
Del: integrated -- https://master.php.net/note/delete/92144/integrated
Del: useless -- https://master.php.net/note/delete/92144/useless
Del: bad code -- https://master.php.net/note/delete/92144/bad+code
Del: spam -- https://master.php.net/note/delete/92144/spam
Del: non-english -- https://master.php.net/note/delete/92144/non-english
Del: in docs -- https://master.php.net/note/delete/92144/in+docs
Del: other reasons-- https://master.php.net/note/delete/92144
Reject -- https://master.php.net/note/reject/92144
Search -- https://master.php.net/manage/user-notes.php