1

I am working on apache sever and generating some tmpdir using follwoing code.

$tmpdatadir = "/home/user/tmpdata/".$id."/";

if (mkdir($tmpdatadir)) {
        /* do something */

}

dir created:

drwxr-xr-x 2 www-data     www-data        4096 Aug 30 17:16 147257020639481

but when i try to write some data using

exec ("cat file.txt >". $tmpdatadir."sample.txt")

i get following error message.

permission denied. As i copy file as user:user so how can i change permision of directory. I found chown does this but i am not sure how can i change ownership of whole directory.

ankit kumar
  • 75
  • 1
  • 10
  • Use `print shell_exec('whoami');` to see, witch user is running your `cat` line. If it is not (and i say it will not) `www-data` then thats the problem. You have to change the rights of the folder after creating the dir. – JustOnUnderMillions Aug 31 '16 at 14:31
  • yes it is www-data but how can i change permssion after creating dir? – ankit kumar Aug 31 '16 at 14:33
  • [dublicate] http://stackoverflow.com/questions/7261566/recursively-chmod-chown-chgrp-all-files-and-folder-within-a-directory – JustOnUnderMillions Aug 31 '16 at 14:33
  • but i checked for chown user must be superuser but it need password to be entered. System("chown -R ".$user.":".$user." myfolder"); – ankit kumar Aug 31 '16 at 14:56
  • I thing you should do it completly with PHP (and the php functions) , not create the folder with PHP and then do the rest via `system()`*command line* , do it fully with PHP. Because you are the right user `www-data` at this point and can change perms from there. – JustOnUnderMillions Aug 31 '16 at 15:00
  • Or do all only with *command line* and create the directory in the first place with: `system('mdir /folder/')` – JustOnUnderMillions Aug 31 '16 at 15:02
  • Im out, have fun:) – JustOnUnderMillions Aug 31 '16 at 15:05
  • i just checked it needs to be superuser for changing permission so i must use system("sudo chown -R ".$user.":".$user." ".$tmpdatadir); but i cannot enter password that time. – ankit kumar Sep 01 '16 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122369/discussion-between-ankit-kumar-and-justonundermillions). – ankit kumar Sep 01 '16 at 08:04

1 Answers1

0

First of all, please review the permissions for www-data folder with ls /lrt, is that way you can see if your user is able to write on the file.

Then, you can use the command: chmod 666 www-data to change the permission of the file to read and write for all users, in this link you can find the syntax for chmod command and a useful calculator if you want limit other users.
Also I share with you the specific functions for commands chown and chmod. See this site.

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29