move_uploaded_file 给出“无法打开流:权限被拒绝”错误
当尝试在 CentOS 上使用 Apache 2.2 和 PHP 5.3 配置上传目录时,我不断收到此错误。
在 php.ini 中:
upload_tmp_dir = /var/www/html/mysite/tmp_file_upload/
在 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 目录权限:
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
无论我做什么,当我上传文件时,我都会从 PHP 中收到此错误:
警告:move_uploaded_file(images/robot.jpg):无法打开流:第 78 行 /var/www/html/mysite/process.php 中的权限被拒绝
警告:move_uploaded_file():无法将第 78 行 /var/www/html/mysite/process.php 中的“/tmp/phpsKD2Qm”移动到“images/robot.jpg”
如您所见,它从未进行过配置来自有关上传文件的 php.ini 文件。
我在这里做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
更改此文件夹的权限
# chmod -R 0755 /var/www/html/mysite/images/
Change permissions for this folder
# chmod -R 0755 /var/www/html/mysite/images/
试试这个:
打开/etc/apache2/envvars
将
www-data
替换为您的your_username
替换为
Try this:
open /etc/apache2/envvars
replace
www-data
with youryour_username
replace with
即使已经成功运行composer之后,我也遇到了这个相关的问题。我更新了作曲家,当运行
composer install
或phpcomposer.phar install
时,我得到:经过大量研究后发现,之前有关更改文件夹权限的答案有效。现在它们只是略有不同的目录。
在我的安装中,在 OS X 上,缓存文件位于
/Users/[USER]/.composer/cache
中,并且我遇到了麻烦,因为缓存文件归 root 所有。将“.composer”的所有权递归更改为我的用户解决了该问题。这就是我所做的:
然后我再次运行作曲家安装,瞧!
I ran into this related issue even after having already successfully run composer. I updated composer, and when running
composer install
orphp composer.phar install
I got: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:
Then I ran the composer install again and voila!
当 apache 用户(www-data)没有写入文件夹的权限时,就会出现此问题。要解决此问题,您需要将用户放入 www-data 组中。
我刚刚做了这个:
执行这个 php 代码
发现 apache 使用的用户。之后,在终端中执行命令:
它将返回如下内容:
我保留了用户,但将组更改为 www-data
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:It will return something like this:
I kept the user but changed the group to www-data
解决办法就是这么简单。只需右键单击 IMAGE(目标)文件夹,转到属性,单击权限选项卡,然后更改其他人对创建和删除文件的访问权限。
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.
只需将tmp_file_upload的权限更改为755即可
以下是命令
chmod -R 755 tmp_file_upload
Just change the permission of tmp_file_upload to 755
Following is the command
chmod -R 755 tmp_file_upload
试试这个
find /var/www/html/mysite/images/ -type f -print0 | xargs -0 chmod -v 664
Try this
find /var/www/html/mysite/images/ -type f -print0 | xargs -0 chmod -v 664
如果启用了
SELinux
,就会发生这种情况。通过设置SELINUX=disabled
在/etc/selinux/config
中禁用它并重新启动服务器。It happens if
SELinux
is enabled. Disable that in/etc/selinux/config
by settingSELINUX=disabled
and restart the server.我已经尝试了上面的所有解决方案,但以下解决了我的问题
I have tried all the solutions above, but the following solved my problem
对我来说,我必须将文件的所有权更改为 Apache 用户和组的“_www”。
For me, I had to change the ownership of the file to '_www' which the Apache user and group.
这是因为
images
和tmp_file_upload
只能由root
用户写入。为了上传工作,我们需要使这些文件夹的所有者与 httpd 进程所有者相同,或者使它们全局可写(不好的做法)。nobody
将
images
和tmp_file_upload
的所有者更改为nobody
或您在第 1 步中找到的任何所有者。Chmod
images
和tmp_file_upload
现在可以由所有者写入(如果需要)[看来您已经准备好了]。 @Dmitry Teplyakov 回答中提到。有关发生此行为的更多详细信息,请查看手册 http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir,请注意,它还讨论了
open_basedir
指令。This is because
images
andtmp_file_upload
are only writable byroot
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).$ps aux | grep httpd
. The first column will be the owner typically it will benobody
Change the owner of
images
andtmp_file_upload
to be becomenobody
or whatever the owner you found in step 1.Chmod
images
andtmp_file_upload
now to be writable by the owner, if needed [Seems you already have this in place]. Mentioned in @Dmitry Teplyakov answer.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.您还可以运行此脚本来找出 Apache 进程所有者:
然后将目标目录的所有者更改为您所拥有的。使用命令:
然后使用命令
更改目标目录权限。
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:
And then use the command
to change the destination directory permission.
这对我有用。
然后注销或重新启动。
如果
SELinux
抱怨,请尝试以下操作This worked for me.
Then logout or reboot.
If
SELinux
complains, try the following如果您使用的是 Mac OS X,请转到文件根目录或网站的文件夹。
然后右键单击它,获取信息,转到最底部(共享和权限),打开它,将所有只读更改为读写。确保打开挂锁,转到设置图标,然后选择应用到随附的项目...
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...
我想将其添加到之前的建议中。如果您使用的 Linux 版本启用了 SELinux,那么您还应该在shell:
同时通过组或更改目录所有者来授予您的 Web 服务器用户权限。
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:
Along with giving your web server user permissions either through group or changing of the owner of the directory.