/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] chmod problem

hi guys,

Sorry to bother again but im running out of ideas and reading the php.net and this forums didnt help me that much…

I want a function to chmod a bunch of files and folders that I regularly need to chmod (and very often forget about some of these…)

So, im getting ‘Operation not permitted’ and ‘file not found’, although im sure the files/folders are there…

Here comes my code, I know echoing everything is not the better way but it is just for debugging…

[code=php]
//This array to include all the files and folders to chmod
$ToChMod = array(
‘admin/’,
);

function MyChmod($item, $mode=’0777′)
{
// This function gets an $item to be chmoded to $mode, if no $mode is specified then
// the default is 0777
// It checks if the $item is an array and changes all the values in it
// This function excludes the . and .. resulting of the directory structure

// Possible inputs: a Directory, a File or an Array. If its a directory or a file
// then it is handled just the same

$output=”;
$path_parts = pathinfo(‘chmod.php’);
$PathToHere = getcwd();//’/home/mg46/public_html’;//pathinfo($_SERVER[‘PHP_SELF’]);
echo $PathToHere.'<br><br>’;
if(!is_array($item))
{
chmod($PathToHere.$item,$mode);
}//if is not array

else
{
// then it is an array
foreach($item as $index)
{
$changed=TRUE;
if($index != ‘.’ && $index!=’..’)
{
echo $index.'<br>’;
chmod($path_parts.$index,$mode) or ($changed=FALSE);
}
if($changed==TRUE){$output.=$index.’ changed to ‘.$mode.'<br>’;}
if($changed==FALSE){$output.=$index.’ was <b>not</b> changed <br>’;}
}
}
echo $output;
return $output;
}//function

if(chmod(__FILE__,0777)){echo ‘Changed<br>’;}
else {echo ‘Not changed<br>’;}

// __FILE__;//pathinfo($_SERVER[‘PHP_SELF’]).'<br>’;

echo getcwd().’ <<<<br><br><br>’;

MyChmod($ToChMod);
[/code]

I cant even get to chmod the own running script…
So whatever suggestion you may have it will be very welcome ?

thank you

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 13.2006 — Remember that your PHP script is normally run under a user ID that is specific to the web server, such as "nobody" or "www" or "apache"; it is not run under your personal user ID. Therefore, if the files are owned by you (your personal login account), only you (or the server's superuser) can chmod them - the webserver user will not be allowed to do so.
Copy linkTweet thisAlerts:
@purefanauthorMay 13.2006 — yes I had forgot about it,

but even when I tried changing the owner to my login information (the ftp username I logged in with when uploading files) it throws me a
Warning: chown(): Operation not permitted in /home/myuser/public_html/chmod.php on line 37
[/quote]


what I did for changing owner was:
[code=php]
if($index != '.' && $index!='..')
{
echo $index.'<br>';
if(chown($index,'myuser')){echo "--Chown worked for:".$index;}
chmod($path_parts.$index,$mode) or ($changed=FALSE);
}
[/code]


so im stuck here too...

I

echo exec("whoami");

and found out that the script is running as 'nobody',

so I tried [code=php]
if(chown(__FILE__,'root')){echo "--Chown worked for: ".__FILE__;}
echo exec("whoami");[/code]

but it didnt work -> Operation not permitted in /home/mg46/public_html/chmod.php on line 10

So once again im stuck...
Copy linkTweet thisAlerts:
@NogDogMay 13.2006 — I believe chown can only be done by the superuser.
Copy linkTweet thisAlerts:
@purefanauthorMay 15.2006 — I got it, the thing was fixed when I ftp to the server,

im using it now as a mass chmoder!

You might improve it [especially the output... hehehe]
[code=php]
function MyChmod($path, $mod, $ftp_user,$ftp_pass,$ftp_root,$ftp_serv)
{
/*
$path must be an array and every parameter is mandatory
*/

// set up basic connection
$conn_id = ftp_connect($ftp_serv);

// login with username and password
//$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected as ".$ftp_user." @ ".$ftp_serv."n<br>";
} else {
echo "Couldn't connect as $ftp_usern";
}

// try to chmod $path directory
foreach ($path as $i){
if (ftp_site($conn_id, 'CHMOD '.$mod.' '.$ftp_root.$i) !== false) {
$success=TRUE;
echo '-Changed: '.$i.' to: '.$mod.'<br>';
}
else {
$success=FALSE;
echo '** Failed to chamod: '.$i.'<br>';
}}

// close the connection
ftp_close($conn_id);
return $success;
}
[/code]
Copy linkTweet thisAlerts:
@WebnerdMay 15.2006 — bump..nevermind
×

Success!

Help @purefan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...