Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm sure the answer to this is pretty simple, and it might not be PHP-specific, but I'm trying to use PHP's copy() function to copy an image from one user to another as part of a custom CMS setup.

For example:
copy('/home/user1/public_html/images/image.png', '/home/user2/public_html/images/image.png');
Which returns the usual:
[function.copy]: failed to open stream: Permission denied

I've tried:
-CHMOD user2's "images" folder to 777 (just to test, didn't work)
-Create a new system group, put both users in it, and CHGRP the folder to that group's id. Also tried putting users "apache" and "nobody" in the same group but to no effect.

open_basedir is not enabled/configured (not sure if that matters) and PHP isn't in safe mode. PHP5 handler is suPHP and suEXEC is on. Running WHM 11.3 on a CentOS 5.6 VPS box. I'm sure the solution isn't that difficult but I've searched all around and can't find the answer. Thanks!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

PHP scripts typically run as the web server user. Verify that the web server user has permission to read the source file and write to the destination directory.
Additionally if you are using Linux verify that your SELinux settings do not prevent the web server from diddling about outside the webroot (see the first user-contributed note on the copy() function manual page).

Also note that in the general case doing this is a bad idea -- The reason SELinux settings are so anal about not letting the web server muck about outside the webroot is that it contains the damage if there is a bug in your PHP script.
Bad Things™ can happen if the web server can read from / write to random filesystem locations. If your PHP code isn't properly written & secured these Bad Things™ can include server compromises and substantial loss of time/income.
(In other words, be absolutely certain you need to do this before you do it :-)

share|improve this answer
    
Thanks for the tip, this is pretty interesting. However after checking it out I don't think this is the issue. When I try the setenforce command I get "SELinux is disabled" and I can't enable it. Not sure if that's because I'm on a VPS or what. –  user329280 Jan 11 '12 at 9:02

A good way to troubleshoot these sort of permission issues is to sudo -u apache (or whatever apache is running under) and try to copy the file yourself:

sudo -u apache cp -a /home/user1/public_html/images/image.png /home/user1/public_html/images/image.png

Does this work?

share|improve this answer
    
I can manually copy it that way if I sudo as nobody, which Apache runs under. However from inside of my PHP app if I try exec('whoami') it returns user1. I'm confused. All three users, user1, user2, and nobody have all been added to my new group which is the group owner of the folder. Does the folder's group even matter? –  user329280 Jan 11 '12 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.