Coder Perfect

The error “failed to open stream: Permission refused” is returned by move uploaded file.


When I try to configure the upload directory on Cen with Apache 2.2 and PHP 5.3, I keep getting this problem.

In php.ini:

upload_tmp_dir = /var/www/html/mysite/tmp_file_upload/

In httpd.conf:

Directory /var/www/html/mysite/tmp_file_upload/>
    Options  -Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
<Directory /var/www/html/mysite/images/>
                Options -Indexes

CentOS directory permissions:

drwxrwxr-x 2 root root 4096 Nov 11 10:01 images
drwxr-xr-x 2 root root 4096 Nov 12 04:54 tmp_file_upload

When I upload the file, no matter what I do, PHP gives me the following error:

As you can see, the upload file configuration from the php.ini file was never taken into account.

I’m not sure what I’m doing wrong here.

Asked by user63898

Solution #1

This is due to the fact that only the root user has access to pictures and tmp file upload. We need to make the owner of those folders the same as the httpd process owner OR make them globally writable for upload to work (bad practice).

Answered by Laith Shadeed

Solution #2

You may also use this script to determine who owns the Apache process:

php echo exec(‘whoami’); php echo exec(‘whoami’); php echo exec(‘whoami’); php echo exe

After that, set the owner of the target directory to the one you have. Use the following command:

chown user destination_dir

Then execute the command.

chmod 755 destination_dir

to alter the permissions on the target directory

Answered by twlkyao

Solution #3

This was effective for me.

sudo adduser <username> www-data
sudo chown -R www-data:www-data /var/www
sudo chmod -R g+rwX /var/www

Then log out or restart your computer.

Try the following if SELinux complains.

sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www(/.*)?'
sudo restorecon -Rv '/var/www(/.*)?'

Answered by Junius L.

Solution #4

If you’re using Mac OS X, navigate to your website’s file root or folder.

Then right-click on it, go to get information, scroll down to Sharing & Permissions, open it, and change all read-only permissions to read and write. To apply to the enclosed objects, open the padlock, navigate to the setting icon, and select Apply to the enclosed items…

Answered by hawkar ITstudent

Solution #5

This is something I’d want to add to the list of previous suggestions. If you’re using a SELinux-enabled version of Linux, you should also run this command in a shell:

chcon -R --type httpd_sys_rw_content_t /path/to/your/directory

Along with granting user permissions to your web server, either through a group or by altering the directory’s owner.

Answered by Chris

Post is based on