views:

61

answers:

2

When I create a directory with PHP's mkdir(), the folders' owner is 'apache'. When I create a directory over FTP, the owner is 'ftpadmin'.

For some strange reason, with PHP's rename() function I can't move any files into a directory owned by 'apache'. I've chmod() both the file and the directory to 777 and 0777, but 'apache' refuses to move any files into directories it created. I can have 'apache' move files into an 'ftpadmin' directory. I've tried chmod() and mkdir() with 777, 0777, 755, 0755, 1777, 01777 and more.

The directories need to be dynamically generated by my client, and files need to be moved in and out.

The server is a dedicated-virtual server that I have can ssh into and have admin rights to.

Please help! Thanks, Nick

A: 

PHP is running as user Apache. Your FTP account is running as user 'fpadmin'. This is why the owners are different. Have you tried chown() php function (http://php.net/manual/en/function.chown.php)? This will change the owner of a file/directory.

Cheers


Ugh, my first down vote. That stings.. That was a stupid answer though..

Alright, so I'm trying to understand your question/replicate your error. Here's what I've tried and here are my results. Hopefully it will at least be of some use to you.

I have the following directory structure:

/var/www/public_html/test (chmod = 777 via SSH, owned by 'ftpuser')
/var/www/public_html/test/index.php (chmod = 777 via SSH, owned by 'ftpuser')
/var/www/public_html/test/ftp_owned.file (chmod = 644 (default), owned by 'ftpuser')

Then in the index.php file I have the following code:

mkdir("./downvoted");
chmod("./downvoted", 0777);
$h = fopen("./i_am_so.sad", "x+");
fwrite($h, "attempting to redeem myself");
fclose($h);
rename("./i_am_so.sad", "./downvoted/i_am_so.sad");
rename("./ftp_owned.file", "./downvoted/ftp_owned.file");

Resulting in a new directory structure:

/var/www/public_html/test (chmod = 777 via SSH, owned by 'ftpuser')
/var/www/public_html/test/index.php (chmod = 777 via SSH, owned by 'ftpuser')
/var/www/public_html/test/downvoted (chmod = 0777 via PHP, owned by 'apache')
/var/www/public_html/test/downvoted/i_am_so.sad (permissions = 644 (default), owned by 'apache')
/var/www/public_html/test/downvoted/ftp_owned.file (permissions = 644 (default), owned by 'ftpuser')

Still sad as I wasn't able to reproduce the error nor redeem myself. Hopefully you can either post the error you are receiving (permission denied?) or point out how my experiment differs from your application.

Mahdi.Montgomery
chown from Apache won't change anything on any system which is properly set up.
unbeli
You can't "give away" files with chown, so you couldn't chown an ftpadmin file to apache. Only root is allowed to do that.
Marc B
yes, thanks, I did know that I just wasn't thinking. upvoted to put emphasis on my misinformation.
Mahdi.Montgomery
Yeah, the chown() only works for super user.I did execute your code, and it worked fine locally
Nick
Oops, this is my first time on stackoverflow; didn't mean to submit my last comment. I was saying that it worked locally, but not on my server. It created the directory and the file, both are owned by 'apache', but they are both in the root directory -- i_am_so.sad doesn't get moved to /downvoted/ . Maybe there is a setting in apache conf or php.ini?
Nick
What is the output of your apache error log? It should log an error. That error will help to isolate the issue. If this is a VPS with plesk, the error log will be located in /var/www/vhosts/domain.com/statistics/logs or similar.
Mahdi.Montgomery
I downloaded my error_log, but it didn't seem to report any PHP errors - just 404 errors, etc. I also set the PHP.ini error reporting to E_ALL and restarted the server, the error was "Safe Mode Restriction in Effect" and that the directory / file had different uids...
Nick
A: 

Perhapse safe-mode features? http://www.php.net/manual/en/features.safe-mode.functions.php

rename() Checks whether the files or directories being operated upon have the same UID (owner) as the script that is being executed. Checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.

If safe mode is on, turn it of, and solve the functionality it provides at a proper level. If this isn't the problem, showing the actual error php undoubtedly emits could help us a lot.

Wrikken
Thanks! I set the php.ini error reporting to E_ALL, and this is exactly the error that was returned. The curious thing is that I'm having PHP ("apache" user) create the directories AND the files (or uploads), but for some reason the folders and files are being assigned to different users...
Nick