Page 1 of 1

I little basic chmod code help plz

Posted: Wed Jul 01, 2009 3:20 pm
by gilesrulz
Hiya,

I'm a flash developer, but I don't know much (any) PHP. Could someone give me a little help with this code?

I understand that I should have permissions to chmod the file, because it was created with a PHP script. What I want to do is:

if the 'state' variable, passed from flash, = 0, chmod the file to 0444 (read only) then set 'state' to 1 and send back to flash
if the 'state' variable, passed from flash, = 1, chmod the file to 0644 (write/read) then set 'state' to 0 and send back to flash

I'm also looking for a separate piece of code that will check what the chmod is and report back where 0644 = 0, and 0444= 1

This is what I have for the first script but I know it's not right, because I only want the 'state' to change if the chmode returns true.

Thanks for your help!

Code: Select all

<?php
//
$state = $_POST['state'];
 
if ($state == 0){
    chmod("file.xml",0444);
    $state = 1;
}else if ($state == 1){
    chmod("file.xml",0644);
    $state = 2;
}else {
//make it fail
}
echo $state;
?>

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 3:33 pm
by BornForCode
You may check the file permission using this:

Code: Select all

 
clearstatcache();
$permission = substr(sprintf('%o', fileperms($path)), -4); 
if($permission == 0884) {
//do something
}
 

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 5:16 pm
by requinix
BornForCode wrote:You may check the file permission using this:

Code: Select all

 
clearstatcache();
$permission = substr(sprintf('%o', fileperms($path)), -4); 
if($permission == 0884) {
//do something
}
 
That works? Doesn't look like it to me.

Code: Select all

$permission = fileperms($path);
if ($permission == 0644) {
} else if ($permission == 0444) {
} else {
}

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 5:23 pm
by Eric!
you need one more equal sign in there

Code: Select all

$permission = fileperms($path);
if ($permission == 0644) {
} else if ($permission == 0444) {
} else {
}

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 5:39 pm
by gilesrulz
I wasn't able to get it to work either..
The output of the following code is 0file.xml04443 so it sees the permission, but doesn't set the variable correctly.

Code: Select all

<?php
 
//setstate
$state = $_POST['state'];
$path = "file.xml";
echo($state);
echo($path);
clearstatcache();
 
$permission = substr(sprintf('%o', fileperms($path)), -4);
echo($permission);
if($permission == 0644) {
$state = 0;
}else if($permission == 0444) {
$state = 1;
}else{
$state = 3;
}
 
echo($state);
?>

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 5:55 pm
by requinix
Eric! wrote:you need one more equal sign in there
I blame the keyboard. Really, it's been weird the last couple days...

fileperms will return a number. With sprintf and %o you get that number in octal without the leading zero. substr counting from -4 will give up to four digits but not necessarily four.
If the file has permissions 0644 then $permission might be "0644".
You then compare it to the octal number 0644, which is actually 420 in decimal.

Code: Select all

if ("0644" == 420) {
The problem should be apparent from that.

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 6:04 pm
by gilesrulz
I don't know if you're trying to help me or not, but I have no idea what you're getting at.

I just want to be able to check the permissions, change the permissions, verify those permissions and return a variable that indicates those permissions.

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 7:14 pm
by requinix
Eric and I have posted code. Yes, you tried Eric's, but there was a little problem with it.

Even if they're not PHP developers we tend to make people think some before handing them a solution. If you want it fixed then pay somebody: if you want to fix it then try to be patient.

You can use fileperms() to get the file/directory permissions. To set permissions you already know to use chmod().

Re: I little basic chmod code help plz

Posted: Wed Jul 01, 2009 11:07 pm
by Eric!
I think the octal/decimal comparison might have been a little hard to grasp at first. Lots of people have never heard of octal.

What tasairas is saying is try this

if($permission == "0644")
or this
if($permission == 420)

They are the same statement. The trick is to figure out why and how.

Re: I little basic chmod code help plz

Posted: Thu Jul 02, 2009 12:32 am
by requinix
Eric! wrote:What tasairas is saying is try this

if($permission == "0644")
or this
if($permission == 420)

They are the same statement.
Actually no, they're not the same. "0644" is a string, 420 is a number. When PHP converts "0644" into a number it'll end up as 644. Or if you think it converts $permission to a string (could be, could be) then the result will be "420".

Either way the first one won't work; the second will.

Re: I little basic chmod code help plz

Posted: Thu Jul 02, 2009 8:20 am
by Eric!
tasairis wrote: Actually no, they're not the same.
There you go, I missed php's conversion as well. I forget that php doesn't care what you're doing and does literal conversions. Now he's either given up or understands what's going on....