Here is my php code to check uploaded file:
<?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");
?>
What I want to is to change the visibility of the user image even if the user has not selected a file to upload. The alerts show up correctly when there is no file selected. But when I give a wrong file, (i.e. a .txt file for instance page still shows "Profile picture visibility updated" instead of the expected result "Invalid file. Only image files are allowed"
What am I doing wrong?
i just tested your code, its messy and i dont believe it will really work, however your not getting the error message because of the following:
when the file is updated, you use
setcookie(..)which I dont believe is the right way to achieve printing out the “success message”, then you reload the page for the user so it loads directly to afterif $_FILES and if $_POSTcheck.you then check if this cookie exist you print its value and then you try to unset this cookie, and at this point your code fails, because you CAN NOT send headers (setcookie, header(), session()) if anything is printed in the page.
now if you fixed that it would not work either, because you are submitting the image file and the image privacy
$_FILES and $_POSTboth in the same request, so if$_FILESfails the$_POSTrequest will succeed and it will reload the page and the error variables will be lost.I dont know why would you use the
header("location:...")function on upload success, you dont want the user to resubmit the data if he reloads the page? its not an issue and not even a security issue if it is for setting cookies with the success msg and displaying them, there are better ways.i quickly tweaked your code, test if it works for you, and NOTE this is not the best way to do it the right way, im only providing you this so you can learn the basic structure for handling forms in PHP so you can (must) use them in functions and classes