CHMOD Problems...

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
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

CHMOD Problems...

Post by Mr. Tech »

Hi!

I want to be able to chmod files online. I used: chmod ("test.html", 0755); but it gives me this error:

Warning: chmod(): Operation not permitted in /home/uname/public_html/chmod.php on line 2

Do you know why this is happening?

Thanks :D
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

better check whether you have rights to do that
check the rights for the dirctory too in which this file resides
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

What do you mean "rights"? The folder it is in needs to be writeable?

In my actual script the file I am chmoding in a parent folder... if that helps..

Thanks
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Mr. Tech wrote:What do you mean "rights"? The folder it is in needs to be writeable?
Files and folders permissions are stored for three user ids: owner, group and "anybody" - hence the three numbers eg 755.

Owner has 1 (read) + 2 (write) + 4 (execute) = 7
Group has 1 (read) + 4 (execute) = 5
Anybody has 1 (read + 4 (execute) = 5

Normally only the owner is granted full rwx permissions: 755 restricts other users. You can view owner/groups with your ftp program.

In many setups, files and folders created by php have php/php owner/group and so php would have rwx permissions with a 755 value.

If a file or folder was not created by php, it will not be owned by php, and hence php has the "anybody" permissions: r-x with 755.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

So php can only chmod the files/folders it created?

Bit confused...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

more or less that's true.
it depends on the user id under which a file/directory has been created.
As McGruff pointed out, only the owner (and root) can change the permissons (wouldn't make too much sense otherwise, would it ;) ).
Your php script runs under the permissons of a certain uid, unless configured otherwise that's the uid of the webserver. On unix-like systems that's often the user nobody or apache.
If you script creates a file/directory this uid is the owner and therefor can change the permission. If you create it via ftp with another uid (often according to your login) then php is not able the change the permissions.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

That is really stupid... Can I change pernissions...
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Sorry, I meant can I change the owner(user id) :lol:
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

You can FTP to the server and change permissions to the folder.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

with shell access and the system not configured to only allow root to change file ownership it's really simple.

Code: Select all

chown -r <newowner> <directory>
and the whole directory together with its content and subdirectories belongs to newowner.
But only few systems allow SITE CHOWN for ftp and even less allow this to users other than root and only few and far between are those that will do it for a complete directory structure.
You could write a php-script that makes a deep copy of the directory.

And if you're lucky I'm all wrong and there is a much simpler solution ;)
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

There must be an easy way... Otherwise how would those file managers work?

I found this filemanager:

http://webmanager.sourceforge.net/

And it had a permission file... Maybe it will help:

Code: Select all

<?php
include("config.php");
include("func.php");
if (!isset($man_user)||(isset($man_user)&&$man_user==1))
{
	include("login.php");
	exit;
}

$ThisFileName = basename(__FILE__);
$root = str_replace($ThisFileName,"",__FILE__);
$root = dirname($root);

/////////////////////
//write = 2
//execute = 1
//read = 4
if (@$perms=="Set")
{
$own_exec=0;$own_read=0;$own_write=0;
$grp_exec=0;$grp_read=0;$grp_write=0;
$other_exec=0;$other_read=0;$other_write=0;
/////////////////////////////////////////////////////////////////////////////////
if (@$set_own_exec==true){$own_exec=1;}
if (@$set_own_read==true){$own_read=4;}
if (@$set_own_write==true){$own_write=2;}
/////////////////
if (@$set_grp_exec==true){$grp_exec=1;}
if (@$set_grp_read==true){$grp_read=4;}
if (@$set_grp_write==true){$grp_write=2;}
/////////////////
if (@$set_other_exec==true){$other_exec=1;}
if (@$set_other_read==true){$other_read=4;}
if (@$set_other_write==true){$other_write=2;}
/////////////////
$set_own=$own_exec+$own_read+$own_write;
$set_grp=$grp_exec+$grp_read+$grp_write;
$set_other=$other_exec+$other_read+$other_write;
////////////////////////////////////////////////////////////////////////////////
$chmode=$set_own.$set_grp.$set_other;
$buf=intval($chmode/100);
$buf1=$chmode-$buf*100;
$buf_0=intval($buf1/10);
$buf2=$buf1-$buf_0*10;
////////////////////////////////////////////////////////////////////////////////
if ($file_array[0]!="")
{
	$dec1=1*32768+0*4096+0*512+$buf*64+$buf_0*8+$buf2*1;
	for($a=0;$a<count($file_array);$a++)
	{
		if (@$folder)
		{
			$file=$root."/".$folder."/".basename($file_array[$a]);
		}
		else
		{
			$file=$root."/".$file_array[$a];
		}
    chmod($file, $dec1);
	}
}
/////////////////////////////////
if ($folder_array[0]!="")
{
	$dec2=4*4096+0*512+$buf*64+$buf_0*8+$buf2*1;
	$dec1=1*32768+0*4096+0*512+$buf*64+$buf_0*8+$buf2*1;
	for($a=0;$a<count($folder_array);$a++)
	{
		if (@$folder)
		{
			$file=$root."/".$folder."/".$folder_array[$a];
		}
		else
		{
			$file=$root."/".$folder_array[$a];
		}
	chmod($file, $dec2);
    }
}
/////////////////////////////////
}
ret_head($folder);
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

still it needs the permission to change the file attributes.
If files are uploaded through the script (or one running as the same uid) it certainly has.
Post Reply