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

发布于 2024-12-14 20:10:43 字数 956 浏览 1 评论 0原文

当尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

维持三分热 2024-12-21 20:10:44

更改此文件夹的权限

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

Change permissions for this folder

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

一抹淡然 2024-12-21 20:10:44

试试这个:

  1. 打开/etc/apache2/envvars

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

    “导出 APACHE_RUN_USER=www-data” 
    

    替换为

    export APACHE_RUN_USER='your_username' 
    

Try this:

  1. open /etc/apache2/envvars

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

    "export APACHE_RUN_USER=www-data" 
    

    replace with

    export APACHE_RUN_USER='your_username' 
    
丢了幸福的猪 2024-12-21 20:10:44

即使已经成功运行composer之后,我也遇到了这个相关的问题。我更新了作曲家,当运行 composer installphpcomposer.phar install 时,我得到:

...无法打开流:权限被拒绝...

经过大量研究后发现,之前有关更改文件夹权限的答案有效。现在它们只是略有不同的目录。

在我的安装中,在 OS X 上,缓存文件位于 /Users/[USER]/.composer/cache 中,并且我遇到了麻烦,因为缓存文件归 root 所有。将“.composer”的所有权递归更改为我的用户解决了该问题。

这就是我所做的:

sudo chown -R [USER] cache

然后我再次运行作曲家安装,瞧!

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!

爱殇璃 2024-12-21 20:10:44

当 apache 用户(www-data)没有写入文件夹的权限时,就会出现此问题。要解决此问题,您需要将用户放入 www-data 组中。

我刚刚做了这个:

执行这个 php 代码 发现 apache 使用的用户。之后,在终端中执行命令:

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

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

它将返回如下内容:

total of files

drwxr-xr-x 7 user group size date folder

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

chown -R user:www-data yourprojectfoldername

chmod 775 yourprojectfoldername

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
任性一次 2024-12-21 20:10:44

解决办法就是这么简单。只需右键单击 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.

萌逼全场 2024-12-21 20:10:44

只需将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

轮廓§ 2024-12-21 20:10:44

试试这个

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

哽咽笑 2024-12-21 20:10:44

如果启用了SELinux,就会发生这种情况。通过设置 SELINUX=disabled/etc/selinux/config 中禁用它并重新启动服务器。

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

我只土不豪 2024-12-21 20:10:44

我已经尝试了上面的所有解决方案,但以下解决了我的问题

chcon -R -t httpd_sys_rw_content_t your_file_directory

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

chcon -R -t httpd_sys_rw_content_t your_file_directory
孤檠 2024-12-21 20:10:44

对我来说,我必须将文件的所有权更改为 Apache 用户和组的“_www”。

For me, I had to change the ownership of the file to '_www' which the Apache user and group.

云柯 2024-12-21 20:10:43

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

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

    $sudo chown无人 /var/www/html/mysite/images/
    
    $sudo chown没人/var/www/html/mysite/tmp_file_upload/
    
  3. Chmod 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 指令。

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

  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 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/
    
  3. 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/
    
  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_basedir directive.

塔塔猫 2024-12-21 20:10:43

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

然后将目标目录的所有者更改为您所拥有的。使用命令:

chown user destination_dir

然后使用命令

chmod 755 destination_dir

更改目标目录权限。

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.

若相惜即相离 2024-12-21 20:10:43

这对我有用。

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

然后注销或重新启动。

如果 SELinux 抱怨,请尝试以下操作

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

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(/.*)?'
仅此而已 2024-12-21 20:10:43

如果您使用的是 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...

小苏打饼 2024-12-21 20:10:43

我想将其添加到之前的建议中。如果您使用的 Linux 版本启用了 SELinux,那么您还应该在shell:

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

同时通过组或更改目录所有者来授予您的 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文