Reputation: 1329
I've run into a tricky situation.
I have an image upload field and depending on if there's any errors (not right file type, file size too large, duplicate upload) it will throw an error and not let them upload it and it displays a bootstrap alert notifying them.
However, this alert stays there on page refresh or even if they navigate to another page of the site and come back, the alert comes back.
I've tried exporting the error variable to javascript and having it empty after a certain amount of time or when the user clicks the X to close the alert but none of that has worked, the alert still comes back when they navigate back to the page.
I don't think cookies would help because after the image is uploaded the page refreshes to show the picture so if I set the cookie then the page refreshes the browser would see the cookie is set and delete the array before the user can even see it.
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$message[] = "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
$message[] = "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
$message[] = "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
$message[] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$uImageLink = "images/" . basename( $_FILES["fileToUpload"]["name"]);
try
{
$DB_host = "localhost";
$DB_user = "none of";
$DB_pass = "your";
$DB_name = "business";
$DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DB_con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt = $DB_con->prepare("UPDATE `users` SET `Image` = :uImage WHERE `Id` = :uId");
$stmt->bindparam("uId", $uId);
$stmt->bindparam("uImage", $uImageLink);
$stmt->execute();
$user->redirect('http://mysite/yo');
}
catch(PDOException $e)
{
echo $e->getMessage();
}
} else {
$message[] = "Sorry, there was an error uploading your file.";
}
}
Then in my HTML down below:
<?php
for ($i = 0; $i < count($message); $i++) {
echo '
<div class="alert alert-danger">'. $message[$i] . '</div>
';
}
?>
Upvotes: 0
Views: 52
Reputation: 42718
You should not be calling all this code unless there's actually been an upload.
Let's take a look at selected statements from your code, assuming the user has not uploaded an image:
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// you should've seen an undefined index error in your logs, and $target_file now equals "images/"
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// "images/" doesn't have an extension so this is empty
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
// an empty string isn't going to match any of those
$message[] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// oh look, $message is populated now!
for ($i = 0; $i < count($message); $i++) {
echo '
<div class="alert alert-danger">'. $message[$i] . '</div>
';
}
Upvotes: 1