php move_uploaded_file 给出“无法打开流:权限被拒绝”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8103860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:58:31  来源:igfitidea点击:

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

phpuploadfile-permissions

提问by user63898

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

尝试在 CentOS 上使用 Apache 2.2 和 PHP 5.3 配置上传目录时,我不断收到此错误。

In php.ini:

在 php.ini 中:

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

In httpd.conf:

在 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:

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

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

无论我做什么,当我上传文件时,我总是从 PHP 收到此错误:

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

警告: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”

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

如您所见,它从未从 php.ini 文件中获取有关上传文件的配置。

What am I doing wrong here?

我在这里做错了什么?

回答by Laith Shadeed

This is because imagesand tmp_file_uploadare only writable by rootuser. 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).

这是因为images并且tmp_file_upload只能由root用户写入。为了上传工作,我们需要使这些文件夹的所有者与 httpd 进程所有者相同,或者使它们全局可写(不好的做法)。

  1. Check apache process owner: $ps aux | grep httpd. The first column will be the owner typically it will be nobody
  2. Change the owner of imagesand tmp_file_uploadto be become nobodyor 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/
    
  3. Chmod imagesand tmp_file_uploadnow 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/
    
  4. 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_basedirdirective.

  1. 检查 apache 进程所有者:$ps aux | grep httpd. 第一列通常是所有者nobody
  2. 更改imagestmp_file_upload成为nobody的所有者或您在步骤 1 中找到的任何所有者。

    $sudo chown nobody /var/www/html/mysite/images/
    
    $sudo chown nobody /var/www/html/mysite/tmp_file_upload/
    
  3. 文件模式imagestmp_file_upload现在的所有者是可写的,如果需要的话[看来你已经有这个到位。在@Dmitry Teplyakov 的回答中提到。

    $ sudo chmod -R 0755 /var/www/html/mysite/images/
    
    $ sudo chmod -R 0755 /var/www/html/mysite/tmp_file_upload/
    
  4. 有关为什么会发生这种行为的更多详细信息,请查看手册http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir,注意它也讨论了open_basedir指令。

回答by twlkyao

You can also run this script to find out the Apache process owner:

您还可以运行此脚本来找出 Apache 进程所有者:

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

<?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.

更改目标目录权限。

回答by hawkar ITstudent

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

如果您使用的是 Mac OS X,请转到文件根目录或您网站的文件夹。

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 Applyto the enclosed items...

然后右键单击它,转到获取信息,转到最底部(共享和权限),打开它,将所有只读更改为读写。确保打开挂锁,转到设置图标,然后选择应用到所附项目...

回答by 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 SELinuxcomplains, try the following

如果SELinux投诉,请尝试以下操作

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

回答by Chris

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

我想将此添加到以前的建议中。如果您使用的是启用了SELinux的 Linux 版本,那么您还应该在 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.

通过组或更改目录所有者来授予您的 Web 服务器用户权限。

回答by Dmitry Teplyakov

Change permissions for this folder

更改此文件夹的权限

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

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

回答by abaumer

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

即使已经成功运行了composer,我也遇到了这个相关问题。我更新了作曲家,在运行时composer install或者php composer.phar install我得到:

...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.

在我的安装中,在 OS X 上,缓存文件在/Users/[USER]/.composer/cache. 将“.composer”的所有权以递归方式更改给我的用户解决了该问题。

This is what I did:

这就是我所做的:

sudo chown -R [USER] cache

Then I ran the composer install again and voila!

然后我再次运行 composer install ,瞧!

回答by redrider

Try this:

尝试这个:

  1. open /etc/apache2/envvars

    sudo gedit /etc/apache2/envvars
    
  2. replace www-datawith your your_username

    "export APACHE_RUN_USER=www-data" 
    

    replace with

    export APACHE_RUN_USER='your_username' 
    
  1. 打开 /etc/apache2/envvars

    sudo gedit /etc/apache2/envvars
    
  2. 替换www-data为你的your_username

    "export APACHE_RUN_USER=www-data" 
    

    用。。。来代替

    export APACHE_RUN_USER='your_username' 
    

回答by 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.

当 apache 用户 (www-data) 没有权限在文件夹中写入时,会发生此问题。要解决此问题,您需要将用户放入 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:

执行这段php代码<?php echo exec('whoami'); ?>,发现apache使用的用户。之后,在终端中执行命令:

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

我保留了用户,但将组更改为 www-data

chown -R user:www-data yourprojectfoldername

chmod 775 yourprojectfoldername

回答by Sarang

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

只需将 tmp_file_upload 的权限更改为 755 以下是命令 chmod -R 755 tmp_file_upload