CHMOD - can't pass the octal number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bigbroantonio
Forum Newbie
Posts: 5
Joined: Wed Oct 26, 2005 5:43 am

CHMOD - can't pass the octal number

Post by bigbroantonio »

Jcart | Please use

Code: Select all

and

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

and

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]
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

bigbroantonio
Forum Newbie
Posts: 5
Joined: Wed Oct 26, 2005 5:43 am

Post by bigbroantonio »

Jcart | Please use

Code: Select all

and

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

and

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]
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The *1 will be removing the leading 0, as that type casts the variable back into an integer again :)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.) 
?>
bigbroantonio
Forum Newbie
Posts: 5
Joined: Wed Oct 26, 2005 5:43 am

Post by bigbroantonio »

Yes, sorry about the typo!

I'll try removing *1.
bigbroantonio
Forum Newbie
Posts: 5
Joined: Wed Oct 26, 2005 5:43 am

Post by bigbroantonio »

I got some results using

Code: Select all

$chmodsetting = $chmodsetting + 0;
but the leading 0 is lost (so 0755 becomes 755)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Decimal values work as well as octals - so:

Code: Select all

$mode = '0777'; // string
chmod('file.ext', octdec($mode));
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Pretty sure this is what Jenk meant now I look back...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

how about

Code: Select all

$cms = decoct($chmodsetting))*1;
$chmodsetting = '0'.$cms;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bigbroantonio
Forum Newbie
Posts: 5
Joined: Wed Oct 26, 2005 5:43 am

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply