note 87766 added to function.chmod
| From: | sapichartshipco at osu1 dot php dot net | Date: | Sat, 20 Dec 2008 04:18:55 +0000 |
| Subject: | note 87766 added to function.chmod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148379@lists.php.net to get a copy of this message | ||
From the above comment
---------
mmj048 at gmail dot com
04-Oct-2007 08:56
If you want to use a string for the second parameter, including user input, you MUST use octdec.
http://www.php.net/octdec
---------
I would like to tell you why octdec and some issues.
It's only two fact here about do chmod dynamically/ passing the second parameter to it:
1. php variable internally conversion of integer number(OCT->DEC) and
2. chmod internally conversion of parameter which is not immediately/hardcoded OCTAL which always be
converted by suppose any parameter there (both int, string) as Decimal (which should be convert by
chmod before it will applied).
The 1. is that if you do like this $mode = 0755; the leading zero tell php that the nuber here is
Octal. but when it stored. It's just common decimal. (which can be some problem in this chmod
thing) . I will skipped this for now.
The 2. is that when chmod accepted the second parameter , like in the manual said "Note that
mode is not automatically assumed to be an octal value, ..." which really occurred that :
2.1 In case of the immediately/hardcoded number leading with zero like 0755 . This no problem on
it.
2.2 In any other case especially on using a variable there (even function which return value). The
last value to be pass will be always recognized by chmod() as "DECIMAL" which chmod wil be
ALWAYS internally DO CONVERSION on it. the conversion from dec to oct would be done.
*Noted : Any last result will never be a nice octal for the chmod even though you do the decoct() to
the parameter or any function you have . The chmod will ALWAYS convert that again cos it think of
other leading zero hardcoded number as decimal: Trust me or check it yourself!!*
2.3 Any other character which can not be convert to integer should be not applied by chmod()
and in the 2.2 that's WHY we will ALWAYS need to do octdec($mode_we_need_string); because we
just pre-convert it to have chmod convert it back to the mode we really need, and of course octal
internally of chmod itself!!.
And back to the 1. if you accept the mode by Octal value. The stored value in the
parameter/var,Remember it is converted to dec, like $mode_we_need = 0777; -> the stored is 511;
and if you used this into the way of it is / it error again, So whenever we deal with Octal the best
thing is 1. NOT TO USED OCTAL ON OPERATION, only display or accept and used it decimal is OK only,
if you want to used it as it Octal as in chmod by this. It's very troublesome cos php seem not
have some explicitly good handling on octal.
In this chmod() case the solution about issue 1. are:
1.Accept the octal and convert it by decoct() onetime first. That change the value to resemble the
value we need again then follow the solution 2.2.
2.Used string as input : may be the best solution : that is the octdec() kindly accepted string and
also number. The string input can be leading with zero or not. Just a valid number in it which
represent the mode you need. like "0755" or "777" because this will be converted
somehow.
I write this because I didn't get why mmj048 said octdec() and through all the test and
confused until I get it. I realised it complicated or just tricky there, So I want to share here.
*Noted again* Any conversion for the parameter to force the second parameter to be octal so that
chmod will not convert it is useless!
----
Server IP: 69.147.83.197
Probable Submitter: 210.1.12.18 (proxied: 127.0.0.1)
----
Manual Page -- http://www.php.net/manual/en/function.chmod.php
Edit -- https://master.php.net/note/edit/87766
Del: integrated -- https://master.php.net/note/delete/87766/integrated
Del: useless -- https://master.php.net/note/delete/87766/useless
Del: bad code -- https://master.php.net/note/delete/87766/bad+code
Del: spam -- https://master.php.net/note/delete/87766/spam
Del: non-english -- https://master.php.net/note/delete/87766/non-english
Del: in docs -- https://master.php.net/note/delete/87766/in+docs
Del: other reasons-- https://master.php.net/note/delete/87766
Reject -- https://master.php.net/note/reject/87766
Search -- https://master.php.net/manage/user-notes.php