RE: [PHP] umask() and chmod()
| From: | Ford, Mike [LSS] | Date: | Thu, 15 Apr 2004 16:57:37 +0000 |
| Subject: | RE: [PHP] umask() and chmod() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-183492@lists.php.net to get a copy of this message | ||
On 15 April 2004 17:26, David T-G wrote:
> Mike, et al --
>
> ...and then Ford, Mike [LSS] said...
> %
> % On 15 April 2004 16:26, David T-G wrote:
> %
> % > but chmod() needs a permissions setting rather than a umask. % >
> % > The challenge is in representing this as octal. With some ...
> % > to feed to chmod() -- and apparently I can't just % >
> % > $r = 0.$r ;
> %
> % That would be
> %
> % $r = '0'.$r;
>
> Hmmm... OK.
>
>
> %
> % I'm not sure, however, that this is a totally foolproof way
> of doing it, as
> % it would fail with any permission set (however unlikely)
> where the owner
>
> Would it? Suppose I were setting it to 007; that would be 0007 with
> the leading zero and should still be fine.
No. The way you're building it, $r would be an integer before you add the leading zero -- 007
would thus be represented as just 7, and adding the leading zero the way I've shown above would
give '07'. Not good.
>
>
> % odgit was a zero -- perhaps you should be sprintf()-ing it?
>
> Heck, I'll take any advice I can get :-) I think, though, that the
> problem is that I'm trying to use a string -- if I can get it built
> correctly in the first place -- as an octal digit.
Possibly, but I think you're making the whole thing more complicated than it need be. After a
quick look at the manual, I'd suggest this:
$u = umask(); // Integer value of umask -- no need to convert
// to octal representation as that's just a human
// convenience of no value to your computer ;)
$m = 0777; // $m is now an integer corresponding to octal
// 0777 again, the visual representation of this
// in octal (or binary, or hex...) is just a
// human convenience.
$r = $m ^ $u; // remove bits set in $u from $m (subtract should
// work too, but since this is technically a
// bitwise operation, I prefer the bitwise
// operator!
// $r is now the correct value, and just needs representing in octal:
$r = sprintf('%04o', $r);
// Done -- use it as you wish.
Of course, I've been quite verbose there -- the short version is:
$r = sprintf('%04o', 0777 ^ umask());
... ;))
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211