【问题标题】:Getting permission denied when uploading image using php on linux server在 linux 服务器上使用 php 上传图像时获得权限被拒绝
【发布时间】:2014-01-16 22:22:33
【问题描述】:

我有一个上传图片的 php 脚本。但是我收到了这个错误:

12-29 21:48:13.314: D/IMAGE SAVE(842): <br /><b>Warning</b>:  unlink(./../images/avatars/1/File_1380293066605.png): Permission denied in <b>/var/www/project/scripts/setimage.php</b> on line <b>21</b><br />
<br /><b>Warning</b>:  move_uploaded_file(./../images/avatars/1/File_1388371693147.png): failed to open stream: Permission denied in <b>/var/www/project/scripts/setimage.php</b> on line <b>27</b><br />
<br /><b>Warning</b>:  move_uploaded_file(): Unable to move '/tmp/phpNzujoE' to './../images/avatars/1/File_1388371693147.png' in <b>/var/www/project/scripts/setimage.php</b> on line <b>27</b><br />

有谁知道我该如何解决这个问题?

PHP 代码

<?php

    ini_set('display_errors',1);
    error_reporting(E_ALL);

    if (isset($_FILES['uploadedfile'])) {

        $userID = $_GET['userID'];
        include dirname(__FILE__).'/updatelastactive.php';
        UpdateLastActive($userID);

        $targetID = $_GET['targetID'];

        $target_path = "./../images/avatars/{$targetID}/";

        if (!is_dir($target_path)) {
            mkdir($target_path);         
        } else {
            $files = glob("{$target_path}*");                         // get all file names
            foreach($files as $file) {                                // iterate through all files
                if(is_file($file)) unlink($file);                     // delete file
            }
        }

        $target_path = $target_path . basename($_FILES['uploadedfile']['name']);

        if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
            $errorMessage = "There was an error uploading the file.";
        } else {
            $filename = basename($_FILES['uploadedfile']['name']);

            $query = "update user set avatar_filename='{$filename}' where id={$targetID}";

            mysql_query($query);    
            $sqlerror = mysql_error();
            if ($sqlerror != null) {
                $errorMessage = "Error: An error occured, please try again later.";
            } else {
                $successMessage = new stdClass;
            }
        }
    } else {
        $errorMessage = "Error: Problem reading image.";
    }

?>

谢谢

编辑:我是服务器的 root 用户。

【问题讨论】:

  • 即使您是 root 用户,PHP 脚本也不会以 root 身份运行。
  • 我遇到了同样的问题,但后来我将目录的所有者权限更改为“www-data”作为用户和“root”作为一个组,这对我有用。

标签: php linux permissions image-uploading


【解决方案1】:

一个快速而肮脏的修复方法是 chmod 777 您要上传到的文件夹。可能有更安全的解决方案,但这应该可行。

【讨论】:

  • 如果您指的是/tmp 文件夹,它已经拥有rwxrwxrwt (1777) 权限。
  • 不是 /tmp 文件夹,而是 .../images/avatars 文件夹。
  • 好建议!让网站只有777 权限!系统上的恶意软件脚本和其他用户会喜欢的!但这不是问题。问题是 PHP 脚本在 Apache 下运行,并且被写入的目录需要有某种方式为 Apache 用户提供写入权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2018-08-04
  • 2020-05-24
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多