Changing the group of a folder created by PHP (using mkdir)

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
willothewisp
Forum Newbie
Posts: 2
Joined: Mon Jun 08, 2009 3:49 am

Changing the group of a folder created by PHP (using mkdir)

Post by willothewisp »

Hi,
I've written a script for a client which creates a folder on the server using mkdir. The client wanted this folder to be created for them (don't ask why, it's complicated!), then they wanted to be able to FTP in and add files into the folder using FTP.
The problem is that the script creates the file with a group of 99. When the client tries to FTP in and add their files, they get the following error: '550 Can't create directory: Permission denied', presumably because they're in a different group.
What I need to be able to do is get the script to create a folder with a different group, so that my client can FTP their files into it.
I thought I would be able to use PHP's chgrp after mkdir, as follows:
mkdir($filepath, 0777)
chgrp($filepath, 501);

However then I get "Warning: chgrp() [function.chgrp]: Operation not permitted". I realise from reading the manual that I need to be a superuser to do this, which my web script obviously isn't.
My question is, how can I get around this? Is there some setting in Apache that I can set?

Thanks in advance for any help!

Willo
bigjoe11a
Forum Newbie
Posts: 9
Joined: Mon Jan 17, 2011 2:57 pm
Location: Ohio, USA
Contact:

Re: Changing the group of a folder created by PHP (using mkd

Post by bigjoe11a »

Will, To change the permission of the FTP servers folder. You have to log in the FTP server with a FTP Client.
Find the folder that needs to be changed and right click on it and at the botton it will say premission. Make sure you set the premissions to 777, This means read,Write and execute.

Then try it again. If you still have the same problem. What FTP Client are you using to access the FTP Server.

From what I can read, It looks like they are trying to create the folder again. They will only be able to create the folder, Just one time
When they use the FTP Client to connect. All that have to do is select the folder on the FTP Server that want to upload too and Then select the files on the right window to upload. That's all there is to it


Joe
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Changing the group of a folder created by PHP (using mkd

Post by requinix »

Uh no, chmod can change the permissions on anything PHP already has permissions on. If PHP can create the folder, PHP can chmod it. You don't have to use FTP for it.
It can't, however, chown or chgrp it.

The solution is just changing the permissions to 0777. That should be enough.
bigjoe11a
Forum Newbie
Posts: 9
Joined: Mon Jan 17, 2011 2:57 pm
Location: Ohio, USA
Contact:

Re: Changing the group of a folder created by PHP (using mkd

Post by bigjoe11a »

tasairis wrote:Uh no, chmod can change the permissions on anything PHP already has permissions on. If PHP can create the folder, PHP can chmod it. You don't have to use FTP for it.
It can't, however, chown or chgrp it.

The solution is just changing the permissions to 0777. That should be enough.
that's not what he's saying. he says the people that wanted him to make the PHP code are using an FTP Client to access the same folder and then trying to create the folder again. and then upload the files that wanted to add. You are right, PHP will let you do all that. If there are using an FTP Client to upload the files. Maybe you should get them to use the PHP page to transfer there files. It would be easy, Then FTP any way.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Changing the group of a folder created by PHP (using mkd

Post by requinix »

bigjoe11a wrote:that's not what he's saying. he says the people that wanted him to make the PHP code are using an FTP Client to access the same folder and then trying to create the folder again.
Really? Because I saw
willothewisp wrote:The client wanted this folder to be created for them (don't ask why, it's complicated!), then they wanted to be able to FTP in and add files into the folder using FTP.
ie, the folder exists and the client wants to upload stuff to it (stuff which presumably includes folders, thus the "cannot create directory" error).
bigjoe11a
Forum Newbie
Posts: 9
Joined: Mon Jan 17, 2011 2:57 pm
Location: Ohio, USA
Contact:

Re: Changing the group of a folder created by PHP (using mkd

Post by bigjoe11a »

The one thing he did leave out is that it should check to make sure the folder doesn't exist. If it doesn't, Then create the new folder. and this line

mkdir($filepath, 0777);

It should read like this
mkdir($filepath, 777);

The 0 is not needed.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Changing the group of a folder created by PHP (using mkd

Post by requinix »

bigjoe11a wrote:and this line

mkdir($filepath, 0777);

It should read like this
mkdir($filepath, 777);

The 0 is not needed.
Now that's just plain wrong. The 0 is needed. Without it you get completely different and completely wrong results.

0777 is a different number than 777. Try echoing them out yourself.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Changing the group of a folder created by PHP (using mkd

Post by Benjamin »

tasairis wrote:Now that's just plain wrong. The 0 is needed. Without it you get completely different and completely wrong results.
Yes, the 0 is required because the second parameter needs to be an octal number. Octal numbers are prefixed with a 0.
The mode parameter consists of three octal number components specifying access restrictions for the owner, the user group in which the owner is in, and to everybody else in this order.
http://us.php.net/manual/en/function.chmod.php

Also, just to clarify, it cannot be a string. e.g. This won't work either:

Code: Select all

chmod('somefile', '0777');
willo_the_wisp
Forum Newbie
Posts: 2
Joined: Mon Jan 17, 2011 10:38 am

Re: Changing the group of a folder created by PHP (using mkd

Post by willo_the_wisp »

Guys,
Thanks you all for your responses. For clarity, what I actually said was that the script creates the folder automatically, and sets the permissions to 0777 already.
What's going wrong is that, when the client FTPs in, they get an error saying that they do not have permission to write to the folder. The permissions on the folder are already set to 0777, so using chmod through the FTP client does not help.

However, if you look at the folders using Unix command ls /li, the GROUP of the folder that the PHP script created is 99. It should be 501 (to match all of the other folders). So as far as I can see, I need to get the script to set the group of the folder it creates using CHGRP, as well as setting permissions using CHMOD. Is this possible using a PHP script?

By the way, I am not a 'he'. I am a 'she'! Girls write PHP too! :D

Cheers,

Willo
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Changing the group of a folder created by PHP (using mkd

Post by VladSun »

What's the FTP server installed?
You may execute chgrp (or any other command) as root, by using the sudo command and properly configured sudoers.

PS: willo_the_wisp... Already forgot your password :P ?
There are 10 types of people in this world, those who understand binary and those who don't
willo_the_wisp
Forum Newbie
Posts: 2
Joined: Mon Jan 17, 2011 10:38 am

Re: Changing the group of a folder created by PHP (using mkd

Post by willo_the_wisp »

I have no idea what FTP server is installed. It is my client's server and I don't have SSH access.

I need the PHP script to do the CHGRP at the time it creates the folder. As far as I know, a PHP script cannot do sudo, and even if it could, I don't have the username and password because it's not my server. So my question again is, is it possible to get the PHP script to do the CHGRP?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Changing the group of a folder created by PHP (using mkd

Post by VladSun »

No. Under Apache privileges - no.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply