这是我检查上传文件的 php 代码:
<?php
include("includes/db.php");
include("includes/header.php");
//=========================
//Check file upload
if (!empty($_FILES["file"])) {
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["size"] > 524288000) {
$mtype="error";
$alertc="Image is too large<br/>\n";
$labelc="labeler";
$inputc="er";
}
else {
$imgname = $arrusrselect[id].md5($arrusrselect[id]).$arrusrselect[id].".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "images/user/profile/" . $imgname);
setcookie("success", "Profile picture updated<br/>");
$labelc="label";
$inputc="input";
$upusers=$mysqli->query("UPDATE `users` SET `img`='$imgname' WHERE `id`='$arrusrselect[id]'");
$upimg=$mysqli->query("UPDATE `profile_img` SET `visibility`='$_POST[img_pub]' WHERE `id`='$arrusrselect[id]'");
header('Location: '.$_SERVER['REQUEST_URI']);
}
}
else {
$mtype="error";
$alertc="Invalid file. Only image files are allowed<br/>\n";
$labelc="labeler";
$inputc="er";
}
}
else {
$inputc="input";
$labelc="label";
if (isset($_POST['img_pub'])) {
setcookie("success", "Profile picture visibility updated<br/>");
$upimg=$mysqli->query("UPDATE `profile_img` SET `img`='$imgname', `visibility`='$_POST[img_pub]' WHERE `id`='$arrusrselect[id]'");
header('Location: '.$_SERVER['REQUEST_URI']);
}
}
//check image visibility
//image check complete
//checking complete
$prof_img=$mysqli->query("SELECT `visibility` FROM `profile_img` WHERE `id`='$arrusrselect[id]'");
$prof_img_slct = mysqli_fetch_array($prof_img);
if (($prof_img_slct[visibility]) == "Public") {
$imgchecka = "checked='checked'";
}
elseif (($prof_img_slct[visibility]) == "UsersOnly") {
$imgcheckb = "checked='checked'";
}
else {
$imgcheckc = "checked='checked'";
}
if (isset($_COOKIE['success'])) {
echo "<div id=\"msg\" class=\"success hide\">$_COOKIE[success]</div>\n";
setcookie("success", "", time()-3600);
}
elseif (isset($mtype)) {
echo "<div id=\"msg\" class=\"".$mtype."\">".$alerta.$alertb.$alertc.$alertd.$alerte."</div>\n";
}
echo "<form action='test.php' method='post' enctype='multipart/form-data'>\n";
echo "<table class='login'>\n";
echo "<tr><td class='$labelc'>New Profile Picture:</td><td class='input'><input type='file' name='file' class='$inputc' id='file' /></td><td class='input'> <input type='radio' name='img_pub' value='Public' $imgchecka /> </td><td class='input'> <input type='radio' name='img_pub' value='UsersOnly' $imgcheckb /> </td><td class='input'> <input type='radio' name='img_pub' value='Hide' $imgcheckc/> </td></tr>\n";
echo "<tr><td class='label'></td><td class='input'><p class='flag'> Max. size is 500kB. Allowed file types .jpg, .png & .gif </p></td></tr>\n";
echo "<tr><td></td><td><input type='submit' value='Update' /></td></tr>\n";
echo "</table></form>\n";
include("includes/footer.php");
?>
我想要的是改变用户图像的可见性,即使用户没有选择要上传的文件。当没有选择文件时,警报会正确显示。但是当我给出一个错误的文件时,(即一个 .txt 文件例如页面仍然显示"Profile picture visibility updated"
而不是预期的结果"Invalid file. Only image files are allowed"
我究竟做错了什么?