Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Move_uploaded_file gives “failed to open stream: Permission denied” error

I moved my website to a new server and now my PHP script which was used to create a text file in a directory is not working and is giving the Permission denied error. Please help.
by

2 Answers

iamabhishek
This error is because of permission error. Most probably your Apache web server is running using a different user* and the *htdocs* folder in which the code is saved is created using another user, hence Apache is not allowing PHP to create a file or directory.

1. Check the user for apache process by running the following command in terminal: ps aux | grep httpd. The first column will be the owner typically it will be *nobody* or in the case of bitnami it can be *daemon
.

2. Now change the owner of the directory in which you want to create and move a file. The owner should be the same as we found in the step 1. If the user is daemon, then run the following two commands.


sudo chown daemon /path/to/directory/

where, daemon is the user, then run the below command to change the mode of the folder,

chmod -R 0755 /path/to/directory/

And you should be good to go. Hope this helps.
sandhya6gczb
You can also run this script to find out the Apache process owner:

<?php echo exec('whoami'); ?>

And then change the owner of the destination directory to what you've got. Use the command:

chown user destination_dir

And then use the command

chmod 755 destination_dir

to change the destination directory permission.

Login / Signup to Answer the Question.