每次上传显示一次错误


Show error once per upload

我遇到了一个棘手的情况。

我有一个图像上传字段,根据是否有任何错误(文件类型不正确、文件大小太大、重复上传(,它会抛出错误,不让他们上传,并显示引导警报通知他们。

但是,此警报在页面刷新时会保留在那里,或者即使他们导航到网站的另一个页面并返回,警报也会回来。

我尝试将错误变量导出为 javascript 并在一定时间后将其为空,或者当用户单击 X 关闭警报时,但这些都不起作用,当他们导航回页面时,警报仍然会返回。

我不认为 cookie 会有所帮助,因为在上传图像后,页面会刷新以显示图片,因此如果我设置 cookie,那么页面刷新浏览器会看到 cookie 已设置并在用户甚至可以看到它之前删除数组。

$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.";
        }
}

然后在下面的我的 HTML 中:

<?php
    for ($i = 0; $i < count($message); $i++) {
        echo '
            <div class="alert alert-danger">'. $message[$i] . '</div>
        ';
    }
?>

除非确实有上传,否则您不应该调用所有这些代码。

让我们看一下代码中的选定语句,假设用户尚未上传图像:

$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>
    ';
}