Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access denied - PHP move_uploaded_file - Ubuntu LAMP /var/www

I realize there is some sort of problem with permissions on either my tmp folder or the images folder I created in the /var/www folder. I know that /var/www initially has root access. I have been following some online tutorials to try and fix this issue and have changed my permissions to who knows what over the last hour or so.

I receive this error when trying to upload a file from an HTML form using PHP/MySQL:

Warning: move_uploaded_file(images/verified-gw.gif): failed to open stream: Permission  
denied in /var/www/addscore.php on line 40 Warning: move_uploaded_file(): Unable to move 
'/tmp/phpla4QCP' to 'images/verified-gw.gif' in /var/www/addscore.php on line 40 cannot 
move uploaded file or something beavis

So I think the permissions on either the /var/www/images folder are incorrect or the permissions on the tmp folder are root and the kernel of ubuntu is not letting the php script move from this root owned file to the images folder which has permissions of my user account to my knowledge and is in the group nobody.

I am pretty lost, any help is definitely appreciated.

Ubuntu Linux gnome screenshot of permissions of a folder

Above you can see a picture of the permissions on the images folder I am trying to move the file from the tmp directory to.

Oh and here is the PHP script that fails:

if (!empty($screenshot)) { 
move_uploaded_file($_FILES['screenshot']['tmp_name'], $target) 
or die (' cannot move uploaded file or something beavis'); 

Respectfully,

user

like image 944
user_loser Avatar asked Nov 30 '22 19:11

user_loser


1 Answers

working with USER DIR mod

If this is developer machine, and i think it is, dont try to use /var/www (there will be always permission problem) Try to use user dir

you will keep all your files and projects in your home and there will be no permission problems.

working with Apache root dir

Ok, so if you cant work with user dir, you have to set up file system like this:

1. Find out apache owner user name. If you use Ubuntu, it should be www-data. Go to terminal and type:

$ ps aux | grep apache

You should get something like this below. First column is username.

www-data 2070 0.0 0.0 463244 7788 ? S sty25 0:00 /usr/sbin/apache2 -k start

2. Find out apache user group. Again terminal, type:

$ sudo groups www-data

You should get something like this below. Second column is group name.

www-data : www-data

3. Set RIGHT owner user and owner group for apache root dir:

$ sudo chown -R www-data:www-data /var/www/

4. Set RIGHT privileges for apache root dir:

$ sudo chmod -R 775 /var/www/

Now you have your system cleaned up, and we try to fix your user to have access to /var/www directory.

5. Add apache group to your user:

$ sudo usermod -a -G www-data {YOUR_USER_NAME}

WARNING! dont forget -a switch! this way you append new group to existing list and not replace old groups list with new one.

Now you have read/write access to files located in apache root dir. And we need to tell apache that new files should be readable/writable to group members

6. Add umask command to apache enviroment config:

$ sudo echo umask 002 >> /etc/apache2/envvars

or with graphic editor:

$ gksudo gedit /etc/apache2/envvars

and place umask 002 as last line in this file

7. Voila

$ sudo apache2ctl restart

Good luck!

like image 87
WebHQ Avatar answered Dec 03 '22 11:12

WebHQ