cp: cannot create regular file: Permission denied
/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.
GorTeX
Updated on September 18, 2022Comments
-
GorTeX over 1 year
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?
-
Siva over 5 yearscan you share the result of
whoami
andll /tmp
-
Hamid Yousefi over 5 years@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 -
Siva over 5 years@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. -
GorTeX over 5 years@SivaPrasath whoami is apache and ll /tmp returns drwxrwxrwx for /tmp/foo
-
GorTeX over 5 years@SivaPrasath Enforcing -what does it mean? ...but my problem has been solved by Hamid
-
Siva over 5 years@GorTeX check stackoverflow.com/a/16446586/9702260
-
ctrl-alt-delor over 5 yearsHello 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.
-
-
GorTeX over 5 yearsThanks 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
-
Hamid Yousefi over 5 yearsCan 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. -
GorTeX over 5 yearsphp script: <?php shell_exec(" bash /path/to/script.sh ) ?> and script.sh is #!/bin/bash cp /my/folder/file.txt /tmp/foo
-
GorTeX over 5 yearsHow 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"
-
Hamid Yousefi over 5 yearscan you give me the output of this command:
ls -alh /tmp | grep foo
if the directory hasfoo
name. -
GorTeX over 5 yearsdrwxrwxrwx 4 apache apache foo
-
Hamid Yousefi over 5 yearsjust
rm -rf /tmp/foo
and then add this line before yourshell_exec(" bash /path/to/script.sh )
This one:if (!file_exists('/tmp/foo')) mkdir('/tmp/foo', 744);
test it please. -
GorTeX over 5 yearsNow it is working! Thank you very very much. Still dont understand why it didnt work before but I am glad it does now :)
-
Hamid Yousefi over 5 yearsI really became happy :) you had permission issue that I can't tell you exactly where it made problem. but now your problem solved ;)
-
GorTeX over 5 yearspermissions for the /tmp/foo now is d-wxr-x--T..havent seen that before
-
Hamid Yousefi over 5 yearscheck this answer: askubuntu.com/questions/432699/…