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

May I ask a question about file permission. I set up Ubuntu server where Apache got running. I have simple php upload form and able to upload file to /var/www/site/uploads as follows.

sandbox@sandbox-virtual-machine:/var/www/site/uploads$ ll
total 1736
drwxrwxrwx 2 www-data www-data    4096 Oct 18 02:53 ./
drwxrwxrwx 3 sandbox  sandbox     4096 Oct 18 00:42 ../
-rw-r--r-- 1 www-data www-data  145998 Oct 18 02:53 3d wallpaper pic.jpg
-rw-r--r-- 1 www-data www-data  166947 Oct 18 02:53 3D Wallpapers 9.jpg
-rw-r--r-- 1 www-data www-data 1451489 Oct 18 02:53  
 6453_3d_landscape_hd_wallpapers_green.jpg

Is there anyway to upload files and they show up as

-rw-r--r-- 1 sandbox sandbox  145998 Oct 18 02:53 3d wallpaper pic.jpg
-rw-r--r-- 1 sandbox sandbox  166947 Oct 18 02:53 3D Wallpapers 9.jpg
-rw-r--r-- 1 sandbox sandbox 1451489 Oct 18 02:53     
 6453_3d_landscape_hd_wallpapers_green.jpg

so that I could straight away feed them to waiting/running shell script. Right now waiting script(move,checksums,rename,resize,etc) unable to do anything to uploaded files with attributes of www-data. If I just do as local account, such as

sandbox@sandbox-virtual-machine:/var/www/site/uploads$touch testfile

then the script is able to run as I would like to. Any suggestion would be grateful,thanks in advance as well.

Thanks for everyone giving help to me,that I was able to progress.

Now I am close to getting solved and append the output

sandbox@sandbox-virtual-machine:/var/www/site/uploads$ ll
total 388
drwxrwxrwx 2 www-data www-data   4096 Oct 18 04:22 ./
drwxrwxrwx 3 sandbox  sandbox    4096 Oct 18 04:17 ../
-rw-r--r-- 1 sandbox  sandbox  166947 Oct 18 04:21 3D Wallpapers 9.jpg
-rw-r--r-- 1 sandbox  sandbox  219808 Oct 18 04:20 adafruit_pi.png
-rw-rw-r-- 1 sandbox  sandbox       0 Oct 18 04:22 test

How may I set permission to uploaded files like 'test' only w difference in middle group. Such as adafruit_pi.png Vs test. Which statement shall I insert to php code,please?

share|improve this question

migrated from stackoverflow.com Oct 17 '12 at 21:18

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers 3

PHP is running as a webapp under apache and that is the www-data user that you are seeing.

After upload you can change the owner and group of the files with:

exec( 'chgrp sandbox 3*.jpg' );
exec( 'chown sandbox 3*.jpg' );

Be aware that after changing permissions, the webserver will not be able to write to the files, or delete or move them.

share|improve this answer
    
Alternatively, you can do chown sandbox:sandbox 3*.jpg to combine chown and chgrp –  GlaciesofPacis Oct 17 '12 at 19:41
    
Thanks GlaciesofPacis, how can I edit php to get [ -rw-r--r-- 1 sandbox sandbox 219808 Oct 18 04:20 adafruit_pi.png -rw-rw-r-- 1 sandbox sandbox 0 Oct 18 04:22 test ] ie make the uploaded file's permission same as test file. I think I am away from only one 'w' in the middle. ? –  user1733435 Oct 17 '12 at 20:41
add comment

Try adding user www-data to the sandbox group

share|improve this answer
    
Thank you for help as well,Ertunc. I did tried earlier but could not able to get it work. –  user1733435 Oct 17 '12 at 20:46
add comment

Try changing the user and group for the apache service: Search in the apache configuration file (I think in ubuntu is located in /etc/apache2/apache2.ini) and search for the lines:

User www-data
Group www-data

and change www-data to sandbox.

Notice that the sandbox user should have permissions to Read/Write on the webroot folder.

Hope it helps.

NOTE:This should be implemented for testing purposes, no recommendable for an production environment.

share|improve this answer
    
Thank you for helping me,I could not find apache2.ini but edited the envvars file and able to make progress. –  user1733435 Oct 17 '12 at 20:39
add comment

Your Answer

 
discard

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