...

View Full Version : Chmod question



Jacobb123
03-03-2010, 09:42 PM
I have created class that creates a subdomain and then CHMODS the directory from 755 to 777 so that I can move files over.

Here is the problem that I run into. When I run this from a web browser it fails to CHMOD. If I run it from a terminal as SUDO it does it. Is there a way that I can do the CHMOD from the browser? It would be a heck of a lot easier to do it there then with a CRON job later

My code is below if it helps any. The function I am working with is below




private function setupFilesystem($domain)
{



define('SOURCE_FOLDER', 'removed');
define('CORE_FOLDER', 'removed');
define('DB_SERVER', 'localhost');
define('DB_USER', 'removed'); // This user needs to have permission to create new DB and users
define('DB_PASS', 'removed');
define('DB_SOURCE', 'removed');
// Copy all the files and folders to the account folder and prepare the config files
$userFolder = 'PATH_TO_SUBDOMAIN/'.$domain.'/';


$chmod = `chmod -R 0777 $userFolder`; //This fails when run from a browser
$perms = substr(decoct(fileperms($userFolder)),1);




if(!$this->smartCopy(SOURCE_FOLDER, $userFolder))
die('Error copying the files');
//if the permissions are not set this fails otherwise works fine


}





private function smartCopy($source, $dest, $options=array('folderPermission'=>777,'filePermission'=>777)) {
$result=false;


if (is_file($source)) { # $source is file
if(is_dir($dest)) { # $dest is folder
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$__dest=$dest."/";
$__dest .= basename($source);
}
else { # $dest is (new) filename
$__dest=$dest;
}
$result=copy($source, $__dest);
}
else if(is_dir($source)) { # $source is dir
if(!is_dir($dest)) { # dest-dir not there yet, create it
mkdir($dest);
}
if ($source[strlen($source)-1]!='/') # add '/' if necessary
$source=$source."/";
if ($dest[strlen($dest)-1]!='/') # add '/' if necessary
$dest=$dest."/";

# find all elements in $source
$result = true; # in case this dir is empty it would otherwise return false
$dirHandle=opendir($source);
while($file=readdir($dirHandle)) { # note that $file can also be a folder
if($file!="." && $file!="..") { # filter starting elements and pass the rest to this function again
$result=$this->smartCopy($source.$file, $dest.$file, $folderPermission, $filePermission);
}
}
closedir($dirHandle);
}
else {
$result=false;
}
return $result;
}

angst
03-03-2010, 09:45 PM
your using it wrong,
have a look here at the "php chmod" function: http://php.net/manual/en/function.chmod.php

Jacobb123
03-03-2010, 09:55 PM
It works just fine on CLI but not a browser.....I've tried the other way it does not work ethier

angst
03-03-2010, 09:58 PM
well, thats because your browser accesses the page via your web server, and the web server wont run that command.

I use php chmod all the time and it works perfectly,



chmod("/somedir/somefile", 755);


ps; if your on shared hosting making sure that you have permission to run such commands.

Jacobb123
03-03-2010, 10:01 PM
I tried this as well and it did not work


$userFolder = 'PATH_TO_SUBDOMAIN/'.$domain.'/httpdocs/';


//$chmod = `chmod -R 0777 $userFolder`;
chmod($userFolder, 0777);
$perms = substr(decoct(fileperms($userFolder)),1);

echo $perms;

angst
03-03-2010, 10:08 PM
what didn't work? did you get an error?
did you check the permissions on the $domain folder after running this?

also you didn't say, are you on shared hosting? that could be an issue.

angst
03-03-2010, 10:08 PM
.......double post...

Jacobb123
03-03-2010, 10:13 PM
what didn't work? did you get an error?
did you check the permissions on the $domain folder after running this?

also you didn't say, are you on shared hosting? that could be an issue.

I'm not getting an error it just returns false......Chmod only returns true or false so there shouldn't be an error. When I echo out the permissions it still gives me 755 instead of 777. I'm on a dedicated server doing this. I did check the domain folder for this and it is still 755. I think that it is the web server that does not have the right access to CHMOD. I don't know how to change that though or even check.

angst
03-03-2010, 10:22 PM
hmm, and your sure that the pass it's using is 100% correct?

to check which user owns apache ( if thats what your using ), have a look at the apache.conf

Jacobb123
03-03-2010, 10:45 PM
I assume you meant path and not "pass" Yeah....I've verfied it a couple of times....actually I have not tried to verify it from within the script. Let me do that and see if it works.

angst
03-03-2010, 10:48 PM
sorry, typo. I meant "path"

Jacobb123
03-04-2010, 01:24 AM
OK....so I took out all of my code and just did one operation to chmod the directory and it throws this error

Warning: chmod() [function.chmod]: Operation not permitted in...

So it appears that whatever user the web server is using is not allowed to make changes.

I was under the assumption that if safe_mode was off then I would not have this problem.....

How can I solve this issue?

angst
03-04-2010, 01:33 AM
well, before we go that far, have to tried setting the permissions while creating the folder?


mkdir("/path", 0777);

Jacobb123
03-04-2010, 01:45 AM
Well...The problem is that I am creating the directory with the Plesk API....so it automaticly does the directory creation

angst
03-04-2010, 01:51 AM
but I see it in your code?

mkdir($dest);

thats not it?

Jacobb123
03-04-2010, 02:52 AM
but I see it in your code?

mkdir($dest);

thats not it?

That's only if the directory does not exist.....if it does then we only need to CHMOD it

angst
03-04-2010, 03:34 AM
ok, well the problem is that Plesk is creating the folder and as such owns it, and your trying to use the apache owner to modify the permissions on. that wont work.

I've been reading around and some people suggest creating a bash script to CHOWN the folder using a system account after upload.

JAY6390
03-04-2010, 03:59 AM
A bash script would definitely be my method of choice

Jacobb123
03-04-2010, 06:54 AM
ok, well the problem is that Plesk is creating the folder and as such owns it, and your trying to use the apache owner to modify the permissions on. that wont work.

I've been reading around and some people suggest creating a bash script to CHOWN the folder using a system account after upload.

Could you point me to some of those things you read?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum