Page 1 of 1
CHMOD - can't pass the octal number
Posted: Wed Oct 26, 2005 5:51 am
by bigbroantonio
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I'm quite new to all this stuff. I've got a problem POSTing a number from a form into $chmodsetting in order to use it with mkdir. This is the code:
Code: Select all
<?php
$chmodsetting = $_POST["chmodsetting"];
mkdir($directoryname, $chmodsetting);
?>
What happens is that $chmodsetting is recognised as a string (I think), but in order for mkdir to work an octal number is required.
The form posts numbers like 0755 and 0777 etc, but mkdir creates the directory but set wrond chmod value.
I tried with something like this, but it just didn't work:
Code: Select all
<?php
if ($chmodsetting == "0755"){
$chmodsetting == 0755;} //(without the quotes, so it's not read as a string.)
?>
How do I change that value (0777, 0775) into an octal number?
Thanks
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Wed Oct 26, 2005 7:10 am
by Jenk
Posted: Wed Oct 26, 2005 7:29 am
by bigbroantonio
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I tried using decoct() but it would change the values (so if in the form I typed 0777 it would change 0777 into octal- but it already is!) and remove the leading 0.
I even tried
Code: Select all
$chmodsetting = '0'.decoct($chmodsetting))*1;
to append the 0 (giving the decimal value of the octal number in $chmodsetting). No luck.
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Wed Oct 26, 2005 7:34 am
by Jenk
The *1 will be removing the leading 0, as that type casts the variable back into an integer again

Posted: Wed Oct 26, 2005 7:41 am
by n00b Saibot
uhhh....
Code: Select all
<?php
if ($chmodsetting == "0755"){
$chmodsetting == 0755;} //(without the quotes, so it's not read as a string.)
?>
should be
Code: Select all
<?php
if ($chmodsetting == "0755"){
$chmodsetting = 0755;} //(without the quotes, so it's not read as a string.)
?>
Posted: Wed Oct 26, 2005 10:34 am
by bigbroantonio
Yes, sorry about the typo!
I'll try removing *1.
Posted: Thu Oct 27, 2005 10:21 am
by bigbroantonio
I got some results using
Code: Select all
$chmodsetting = $chmodsetting + 0;
but the leading 0 is lost (so 0755 becomes 755)
Posted: Thu Oct 27, 2005 11:01 am
by Maugrim_The_Reaper
Decimal values work as well as octals - so:
Code: Select all
$mode = '0777'; // string
chmod('file.ext', octdec($mode));
Posted: Thu Oct 27, 2005 11:02 am
by Maugrim_The_Reaper
Pretty sure this is what Jenk meant now I look back...
Posted: Thu Oct 27, 2005 11:02 am
by s.dot
how about
Code: Select all
$cms = decoct($chmodsetting))*1;
$chmodsetting = '0'.$cms;
Posted: Fri Oct 28, 2005 4:14 am
by bigbroantonio
OK, I got it (nearly).
The solution lies in Maugrim_The_Reaper's decimal values.
Basically the mistake lies in calling the POSTed chmodsetting value this way:
Code: Select all
$chmodsetting = $_POST["chmodsetting"];
instead of
Code: Select all
$chmodsetting = $_POST['chmodsetting'];
Using this second line of code (not using the double quotes) gives the string a decimal value.
Then, I used:
Code: Select all
mkdir($directoryname, octdec($chmodsetting));
which made the directory using the chmodsetting value.
The only thing I can't get around is using 0777:
$chmodsetting is set as 0777 (I even tested this with an echo) but only returns 0755 on the server. Using ftp client I can 0777, but not using mkdir. Why?
Posted: Fri Oct 28, 2005 4:27 am
by Jenk
It could be (I am not certain) that the php application (or the user/goup that the application is bound to) does not have permission to do so.
Don't hold me to it though.
Posted: Fri Oct 28, 2005 4:44 am
by feyd
it may be more that your particular variant of PHP doesn't like getting a chmod as an argument to the mkdir function. call
chmod() immediately after creating the directory.