4

Dedicated Linux server running debain LAMP.

I run a PHP script (using a browser) which creates a directory (and various sub directories) in a folder on the same server for subsequent shared use using Dropbox.
The directories are created in /home/dropbox/New_Project_Name/new_folders and should be owned by the user 'dropbox'.

However running the php script causes the newly created directories generated by the script to be owned by 'www-data'

What is the best why of either running the php script from the browser so that it generates the new directories with ownership of user and group 'dropbox' or subsequently running a script to check for www-data ownership and recursively changing files and directories to 'dropbox'

Many thanks for any help.

prashant thakre
  • 5,061
  • 3
  • 26
  • 39
Wonder Works
  • 73
  • 1
  • 1
  • 6

3 Answers3

4

Not tested, but after creating the folder, you can run another line of code to change the owner/group

// define user and group
$owner = "dropbox";
$group = "dropbox";
$folder = "/home/dropbox/New_Project_Name/new_folders";

// change the owner and group
chown($folder, $owner);
chgrp($folder, $group);

Keep in mind, that it might throw an error, because there are subfolders and the operation fails. A while loop should solve the problem.

There might be an issues with the permissions, up to the server-config

There is another way to run it recursively with the "exec" command.

you can go like this:

exec("chown -R ".$owner.":".$group." ".$folder);

This will change user and group for the folder and all sub-folders. But beware, using system is "dangerous". You can run any shell-commands. Don't play around with it too much.

DasSaffe
  • 2,080
  • 1
  • 28
  • 67
  • Many thanks - but I get 'Operation not permitted' I guess due to server permissions. First variable in the answer should be $user and not $username. – Wonder Works Aug 13 '14 at 09:26
  • Yep, you are right. i edited variables and gave another approach to solve it – DasSaffe Aug 13 '14 at 09:32
  • OK - I'm trying with the exec option as you show, but trying to understand how I can give the script permission to run like this. I have added this line to bottom of /etc/sudoers.`code` nobody ALL = NOPASSWD: /home/user/public_html/change_owner.php – Wonder Works Aug 13 '14 at 10:21
  • OK - finally got this working (thanks everybody) but adding the following to my /etc/sudoers `www-data ALL=(ALL) NOPASSWD: /bin/chown, /home/sites/public_html/change_owner.php' The contents of the PHP file were as in the answer from DasSaffe – Wonder Works Aug 13 '14 at 14:20
  • chown($folder, $owner); - according to http://php.net/manual/ru/function.chown.php only for files – Sasha Kos May 20 '18 at 21:05
1

OK - finally got this working (thanks everybody) but adding the following to my /etc/sudoers

www-data ALL=(ALL) NOPASSWD: /bin/chown, /home/sites/public_html/change_owner.php

The contents of the PHP file were as in the answer from DasSaffe

Wonder Works
  • 73
  • 1
  • 1
  • 6
0

Here are 3 options:

  1. Use suEXEC.
  2. Connect to localhost ftp as user dropbox and create directories and files this way.
  3. Set up sudo so www-data user can execute this as root without password prompt: sudo chown -R dropbox /path/to/dir, then just use php's exec function.
Marek
  • 7,337
  • 1
  • 22
  • 33