1

I am running a PHP script on my Apache server and from the script I need to copy some files (to run a Bash script that copies files). I can copy to a directory /tmp with no problems, but when I want to copy to /tmp/foo then I get this error:

cp: cannot create regular file '/tmp/foo/file.txt': Permission denied

even though the permissions for the directory /tmp and /tmp/foo are set to the same value.

Do you know what is the problem?

  • 2
    can you share the result of whoami and ll /tmp – msp9011 Sep 3 '18 at 19:07
  • @SivaPrasath /tmp is always for root by default! the two commands results is not related in this case, becase /tmp has all the permissions for all users – Hamid Yousefi Sep 3 '18 at 19:08
  • 1
    @HamidYousefi yes, tmp is for root, but not for /tmp/foo OP might be running the script as a different user with /tmp/foo belongs to. – msp9011 Sep 3 '18 at 19:17
  • @SivaPrasath whoami is apache and ll /tmp returns drwxrwxrwx for /tmp/foo – GorTeX Sep 3 '18 at 19:28
  • 1
    Hello welcome. To make things easier in the future, when asked for clarification, please clarify by amending the question (not by adding a comment), show commands, and output in full. This will help us with answering you question more quickly. Thanks, and happy questioning. – ctrl-alt-delor Sep 3 '18 at 23:08
3

/tmp Directory has all the permissions (read/write) for all users. but if you made /tmp/foo by your own account, it has its permissions just for you! if you want to make it writable for other users (or programs) change its permission with this command:

chmod 777 /tmp/foo

If you have any other files inside this directory from before, add -R flag to above command.

Update: Use this command to change /tmp/foo owner from your own to apache default user:

sudo chown www-data:www-data /tmp/foo -R

also please check your apache2 configuration to see which user it has for running the php scripts.

  • Thanks for the answer, but it doesnt work. I have set the permissions for /tmp/foo to 777 and still I get the same error. When I run the script as a normal user then it is okay. But problem is when I run it from a php script – GorTeX Sep 3 '18 at 19:16
  • Can I see your scripts please!? and are you using php or php-fpm? your problem is, by default php using apache configured user for running scripts, while you made /tmp/foo by your own account! I will update my answer, check it again please. – Hamid Yousefi Sep 3 '18 at 19:19
  • php script: <?php shell_exec(" bash /path/to/script.sh ) ?> and script.sh is #!/bin/bash cp /my/folder/file.txt /tmp/foo – GorTeX Sep 3 '18 at 19:24
  • How can I find which user apache uses for running php scripts? When I put to my php script bash command whoami then I get "apache" – GorTeX Sep 3 '18 at 19:27
  • can you give me the output of this command: ls -alh /tmp | grep foo if the directory has foo name. – Hamid Yousefi Sep 3 '18 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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