note 86646 added to function.umask
| From: | creccles at gmail dot com | Date: | Tue, 28 Oct 2008 06:29:02 +0000 |
| Subject: | note 86646 added to function.umask | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145853@lists.php.net to get a copy of this message | ||
Understanding umask() properly is made more difficult for the novice programmer by the spurious
flitting between UNIX's octal representation of file permissions and PHP's requirements
for (decimal) integer arguments to most of its functions (including umask).
-
Instead of making mental pictures of chains of rwx|rwx|rw- type words and then tying yourself in
cerebral knots manipulating them between the two different number systems, look at the issue as one
involving simple bit patterns:
-
On most hosts, the baseline permission for created data files is the well-known OCTAL 0666, which
means a bit pattern: 110110110.
-
The baseline pattern for new directories is OCTAL 0777, which is 111111111.
-
Both of these are subject to modification by the current system-wide umask setting, which is
normally OCTAL 0022 or, bitwise: 000010010.
-
The mask works like this:
1. INVERT the binary umask
2. Perform an AND operation with the baseline permission pattern
3. Result is the bit pattern which will be applied to your new files or
directories
Example:
System-wide umask: 000010010 (OCTAL 0022)
INVERTED: 111101101
& with baseline: 110110110 (OCTAL 0666)
Result: 110100100
... which is OCTAL 0644.
Let us say you want write permissions on files for your workgroup:
The bit pattern you are aiming for is: 110110100 (OCTAL 0664)
What needs to be AND-ed with our baseline 110110110 to give us this ?
Any AND relationship is reversible, so the answer is given by:
baseline: 110110110 (OCTAL 0666)
target: 110110100 (OCTAL 0664)
result: 111110101
inversion gives: 000001010 (OCTAL 0012)
... which is our desired umask.
Lining up bits in a binary AND is conceptually easier than juggling
with the rw-rwx--x notation when it comes to masks. Remember that
an AND only delivers a binary '1' when a binary '1' is present in BOTH
strings.
Always pass this to PHP in the form:
umask(octdec("0012"));
... so that the umask() function receives a true INT type.
----
Server IP: 208.69.120.35
Probable Submitter: 84.203.214.202
----
Manual Page -- http://www.php.net/manual/en/function.umask.php
Edit -- https://master.php.net/note/edit/86646
Del: integrated -- https://master.php.net/note/delete/86646/integrated
Del: useless -- https://master.php.net/note/delete/86646/useless
Del: bad code -- https://master.php.net/note/delete/86646/bad+code
Del: spam -- https://master.php.net/note/delete/86646/spam
Del: non-english -- https://master.php.net/note/delete/86646/non-english
Del: in docs -- https://master.php.net/note/delete/86646/in+docs
Del: other reasons-- https://master.php.net/note/delete/86646
Reject -- https://master.php.net/note/reject/86646
Search -- https://master.php.net/manage/user-notes.php