note 82751 modified in function.chmod by danbrown

From: Date: Thu, 16 Oct 2008 13:30:40 +0000
Subject: note 82751 modified in function.chmod by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145417@lists.php.net to get a copy of this message
Note that info at rvgate dot nl's chmodnum function produces INCORRECT results. The resutls are base-10 numbers that only LOOK like they are octal numbers. The function also ignores setuid, setgid and sticky bits, and will produce incorrect numbers if such a file is encountered. Instead, this brute-force code works. Maybe there is something more slick, but this isn't too CPU-intensive (note that it assumes you've error-checked that you indeed have a 10-character string!): <?php $permissions = 'drwxr-xr-x'; // or whatever $mode = 0; if ($permissions[1] == 'r') $mode += 0400; if ($permissions[2] == 'w') $mode += 0200; if ($permissions[3] == 'x') $mode += 0100; else if ($permissions[3] == 's') $mode += 04100; else if ($permissions[3] == 'S') $mode += 04000; if ($permissions[4] == 'r') $mode += 040; if ($permissions[5] == 'w') $mode += 020; if ($permissions[6] == 'x') $mode += 010; else if ($permissions[6] == 's') $mode += 02010; else if ($permissions[6] == 'S') $mode += 02000; if ($permissions[7] == 'r') $mode += 04; if ($permissions[8] == 'w') $mode += 02; if ($permissions[9] == 'x') $mode += 01; else if ($permissions[9] == 't') $mode += 01001; else if ($permissions[9] == 'T') $mode += 01000; printf('Mode is %d decimal and %o octal', $mode, $mode); ?> --was-- Note that info at rvgate dot nl's chmodnum function produces INCORRECT results. The resutls are base-10 numbers that only LOOK like they are octal numbers. The function also ignores setuid, setgid and sticky bits, and will produce incorrect numbers if such a file is encountered. Instead, this brute-force code works. Maybe there is something more slick, but this isn't too CPU-intensive (note that it assumes you've error-checked that you indeed have a 10-character string!): $permissions = 'drwxr-xr-x'; // or whatever $mode = 0; if ($permissions[1] == 'r') $mode += 0400; if ($permissions[2] == 'w') $mode += 0200; if ($permissions[3] == 'x') $mode += 0100; else if ($permissions[3] == 's') $mode += 04100; else if ($permissions[3] == 'S') $mode += 04000; if ($permissions[4] == 'r') $mode += 040; if ($permissions[5] == 'w') $mode += 020; if ($permissions[6] == 'x') $mode += 010; else if ($permissions[6] == 's') $mode += 02010; else if ($permissions[6] == 'S') $mode += 02000; if ($permissions[7] == 'r') $mode += 04; if ($permissions[8] == 'w') $mode += 02; if ($permissions[9] == 'x') $mode += 01; else if ($permissions[9] == 't') $mode += 01001; else if ($permissions[9] == 'T') $mode += 01000; printf('Mode is %d decimal and %o octal', $mode, $mode); http://php.net/manual/en/function.chmod.php

« previous php.notes (#145417) next »