在Linux服务器上使用php上传图像时获得权限被拒绝

时间:2013-12-30 02:52:02

标签: php linux permissions image-uploading

我有一个上传图片的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用户。

1 个答案:

答案 0 :(得分:-2)

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