ChatGPT解决这个技术问题 Extra ChatGPT

move_uploaded_file gives "failed to open stream: Permission denied" error

I keep getting this error when trying to configure the upload directory with Apache 2.2 and PHP 5.3 on CentOS.

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>
<Directory /var/www/html/mysite/images/>
                Options -Indexes
</Directory>

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

No matter what I do, I keep getting this error from PHP when I upload the file:

Warning: move_uploaded_file(images/robot.jpg): failed to open stream: Permission denied in /var/www/html/mysite/process.php on line 78 Warning: move_uploaded_file(): Unable to move '/tmp/phpsKD2Qm' to 'images/robot.jpg' in /var/www/html/mysite/process.php on line 78

As you can see, it never did take the configuration from the php.ini file regarding the upload file.

What am I doing wrong here?

775? Maybe your server is running as nobody. Only root can write in this case (your "images" permissions)...
what does it means ? how can i change it ?
Remember that ALL parent directories also need to have the right permissions.

M
Machavity

This is because images and tmp_file_upload are only writable by root user. For upload to work we need to make the owner of those folders same as httpd process owner OR make them globally writable (bad practice).

Check apache process owner: $ps aux | grep httpd. The first column will be the owner typically it will be nobody Change the owner of images and tmp_file_upload to be become nobody or whatever the owner you found in step 1. $sudo chown nobody /var/www/html/mysite/images/ $sudo chown nobody /var/www/html/mysite/tmp_file_upload/ Chmod images and tmp_file_upload now to be writable by the owner, if needed [Seems you already have this in place]. Mentioned in @Dmitry Teplyakov answer. $ sudo chmod -R 0755 /var/www/html/mysite/images/ $ sudo chmod -R 0755 /var/www/html/mysite/tmp_file_upload/ For more details why this behavior happend, check the manual http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir , note that it also talking about open_basedir directive.


Thanks: our old owner had been daemon it is now apache
This fix applies to situations where you might of changed the servers php type from fast_CGI, CGI to Apache_mod as plesk etc.. can continue with original user's permissions not apache. This fixed my issues.
I am having this same error but both the process and folders are owned by jacob (me as it is my local machine) and the folders all have 755 or 775.
I had to restart my apache process sudo service httpd restart after changing permissions. Then it worked :) Instead of changing owner chown I added my apache process to a 'www' group and added these directories to the same 'www' group through chgrp
P
Peter Mortensen

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.


Thanks it works for me. I first used Laith Shadeed's method but I don't get same result when typing ps aux | grep httpd and <?php echo exec('whoami'); ?>. Does anyone know why ?
ps aux | grep https doesn't return web server owner name. This does : ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1 Fron Symfony doc.
Note that in the above command, there should be two spaces between "-d\" and "-f1". If you copy-paste as-is, you may get an error like "cut: bad delimiter".
plus 1 for exec('whoami'). Saved me 30 more minutes. was chowning ubuntu user
should this be www-data? usually
J
Junius L.

This worked for me.

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

Then logout or reboot.

If SELinux complains, try the following

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

saved my life :).. i use GIT post-recive hook to deploy my web, and every time i deploy i get his permission denied error, adding git user to www-data fixed it :) thank you
This is the best answer.
Don't worry! your answer is the best in our hearts. You just saved my life and I do not know how to say thank you!!!!
P
Peter Mortensen

If you have Mac OS X, go to the file root or the folder of your website.

Then right-hand click on it, go to get information, go to the very bottom (Sharing & Permissions), open that, change all read-only to read and write. Make sure to open padlock, go to setting icon, and choose Apply to the enclosed items...


Why are you commenting on the Mac OS when his question is about a Linux system?
Hi Hawkar, thanks for your reply. I'm on mac and your answer solved my issue. Thanks a lot.
Love this answer
@Kmeixner this question about Linux but I had exactly the same issue on my OSX. Thanks for this comment it worked for me after I changed the writing options in the folder /private/var/tmp on my Mac.
This is an older post, but that is exactly what I needed to do. THanks
P
Peter Mortensen

I wanted to add this to the previous suggestions. If you are using a version of Linux that has SELinux enabled then you should also execute this in a shell:

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

Along with giving your web server user permissions either through group or changing of the owner of the directory.


restorecon -R -v /path/to/your/directory probably also needs to be included in this afterwards. access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/…
may very well be true, but i was under the assumption that chcon "changed context" ie it was just changed. what you are looking at uses "semanage fcontext" first which puts it into some settings file "file_contexts.local", however it never changes the context.
@Chris, Thanks man. This solved my problem. Would you please shed some light on the matter? What does this command actually do? I went over the man pages and even the info for chcon and didn't find the value of the type you entered. I'm a bit confused here.
it makes the directory or files readable by the web server (httpd)... i honestly dont want to and probably couldnt explain selinux as i barely have an understanding in it myself... please see nsa.gov/what-we-do/research/selinux/documentation and access.redhat.com/documentation/en-us/red_hat_enterprise_linux/…
D
Dmitry Teplyakov

Change permissions for this folder

# chmod -R 0755 /var/www/html/mysite/images/


did it now its like : drwxrwxr-x 2 root root 4096 Nov 11 10:01 images also on : drwxrwxr-x 2 root root 4096 Nov 12 04:54 tmp_file_upload but still the same error
r
rink.attendant.6

Try this:

open /etc/apache2/envvars sudo gedit /etc/apache2/envvars replace www-data with your your_username "export APACHE_RUN_USER=www-data" replace with export APACHE_RUN_USER='your_username'


And restart Apache ($ sudo service apache2 restart)to see effects in <?php echo exec('whoami'); ?>
P
Peter Mortensen

I ran into this related issue even after having already successfully run composer. I updated composer, and when running composer install or php composer.phar install I got:

...failed to open stream: Permission denied...

It turns out after much research that the previous answers regarding changing permissions for the folder worked. They are just slightly different directories now.

In my install, on OS X, the cache file is in /Users/[USER]/.composer/cache, and I was having trouble because the cache file was owned by root. Changing ownership of '.composer' recursively to my user solved the issue.

This is what I did:

sudo chown -R [USER] cache

Then I ran the composer install again and voila!


J
Jefferson Romano

This problem happens when the apache user (www-data) does not have permission to write in the folder. To solver this problem you need to put the user inside the group www-data.

I just made this:

Execute this php code <?php echo exec('whoami'); ?> to discover the user used by apache. After, execute the commands in the terminal:

user@machine:/# cd /var/www/html

user@machine:/var/www/html# ls -l

It will return something like this:

total of files

drwxr-xr-x 7 user group size date folder

I kept the user but changed the group to www-data

chown -R user:www-data yourprojectfoldername

chmod 775 yourprojectfoldername

P
Peter Mortensen

The solution is so easy. Only right click the IMAGE (destination) folder, go to properties, click the permission tab, and change others access to Create and delete files.


Quickest way BUT, only you are using GUI for FTPing (FileZilla, WinSCP)
S
Sarang

Just change the permission of tmp_file_upload to 755 Following is the command chmod -R 755 tmp_file_upload


S
Simone

Try this

find /var/www/html/mysite/images/ -type f -print0 | xargs -0 chmod -v 664


getting : chmod: missing operand after `664'
S
Srihari Karanth

It happens if SELinux is enabled. Disable that in /etc/selinux/config by setting SELINUX=disabled and restart the server.


M
Mithun Sarker Shuvro

I have tried all the solutions above, but the following solved my problem

chcon -R -t httpd_sys_rw_content_t your_file_directory

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now