【发布时间】: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