PHP 上传权限被拒绝
PHP Upload Premission Denied
我制作了一个上传 PHP 脚本,该脚本运行了 5-6 次...现在,当我将它移动到另一个文件时,它无法运行。我收到这条消息
File is an image - image/png.
Warning: move_uploaded_file(Viruss.png): failed to open stream: Permission denied in /var/www/html/WEB Dictionary/profile.php on line 57
Warning: move_uploaded_file(): Unable to move '/tmp/phpySH8hY' to
'Viruss.png' in /var/www/html/WEB Dictionary/profile.php on line 57
Sorry, there was an error uploading your file.
HTML代码:
<form action="profile.php" method="post" enctype="multipart/form-data">
<input type="file" name="ChangeAvatarInput" id="ChangeAvatarInput"><br/>
<input type="submit" name="ChangeAvatar" value="{$AvatarUpload}">
</form>
PHP代码:
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['ChangeAvatar'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["ChangeAvatarInput"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["ChangeAvatarInput"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["ChangeAvatarInput"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["ChangeAvatarInput"]["tmp_name"], $Username.".".$imageFileType)) {
echo "The file ". basename( $_FILES["ChangeAvatarInput"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
"uploads" 目录的权限是读写,我使用的是 W3Schools 的示例。
您需要对要复制到的目录进行 chmod 或 chown。所以 chmod/chown 'upload' 所以你有权写入它
在终端中:
如果您位于 project/upload 目录中。
sudo chmod-R 0755 *
这将递归地设置文件夹和子文件夹中所有文件的权限。
如果您不在您的项目中,则:
sudo chmod -R 0755 path_to_your_project
有关文件权限,请访问此 link。
更改此行
move_uploaded_file($_FILES["ChangeAvatarInput"]["tmp_name"], $Username.".".$imageFileType)
至
move_uploaded_file($_FILES["ChangeAvatarInput"]["tmp_name"], '/'.$target_dir.$Username.".".$imageFileType)
我制作了一个上传 PHP 脚本,该脚本运行了 5-6 次...现在,当我将它移动到另一个文件时,它无法运行。我收到这条消息
File is an image - image/png. Warning: move_uploaded_file(Viruss.png): failed to open stream: Permission denied in /var/www/html/WEB Dictionary/profile.php on line 57
Warning: move_uploaded_file(): Unable to move '/tmp/phpySH8hY' to 'Viruss.png' in /var/www/html/WEB Dictionary/profile.php on line 57 Sorry, there was an error uploading your file.
HTML代码:
<form action="profile.php" method="post" enctype="multipart/form-data">
<input type="file" name="ChangeAvatarInput" id="ChangeAvatarInput"><br/>
<input type="submit" name="ChangeAvatar" value="{$AvatarUpload}">
</form>
PHP代码:
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['ChangeAvatar'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["ChangeAvatarInput"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["ChangeAvatarInput"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["ChangeAvatarInput"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["ChangeAvatarInput"]["tmp_name"], $Username.".".$imageFileType)) {
echo "The file ". basename( $_FILES["ChangeAvatarInput"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
"uploads" 目录的权限是读写,我使用的是 W3Schools 的示例。
您需要对要复制到的目录进行 chmod 或 chown。所以 chmod/chown 'upload' 所以你有权写入它
在终端中:
如果您位于 project/upload 目录中。
sudo chmod-R 0755 *
这将递归地设置文件夹和子文件夹中所有文件的权限。
如果您不在您的项目中,则:
sudo chmod -R 0755 path_to_your_project
有关文件权限,请访问此 link。
更改此行
move_uploaded_file($_FILES["ChangeAvatarInput"]["tmp_name"], $Username.".".$imageFileType)
至
move_uploaded_file($_FILES["ChangeAvatarInput"]["tmp_name"], '/'.$target_dir.$Username.".".$imageFileType)