I little basic chmod code help plz

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
gilesrulz
Forum Newbie
Posts: 3
Joined: Wed Jul 01, 2009 3:05 pm

I little basic chmod code help plz

Post 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;
?>
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: I little basic chmod code help plz

Post 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
}
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I little basic chmod code help plz

Post 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 {
}
Last edited by requinix on Wed Jul 01, 2009 5:51 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: I little basic chmod code help plz

Post by Eric! »

you need one more equal sign in there

Code: Select all

$permission = fileperms($path);
if ($permission == 0644) {
} else if ($permission == 0444) {
} else {
}
gilesrulz
Forum Newbie
Posts: 3
Joined: Wed Jul 01, 2009 3:05 pm

Re: I little basic chmod code help plz

Post 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);
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I little basic chmod code help plz

Post 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.
gilesrulz
Forum Newbie
Posts: 3
Joined: Wed Jul 01, 2009 3:05 pm

Re: I little basic chmod code help plz

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I little basic chmod code help plz

Post 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().
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: I little basic chmod code help plz

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I little basic chmod code help plz

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: I little basic chmod code help plz

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