重新加载页面后显示警报/成功消息

时间:2020-06-18 08:05:11

标签: php

我创建了一个表单来用php上传文件,但是当添加文件并刷新页面时,用户仍然可以看到先前操作的结果。我只创建了一个简单的表单,如下所示:

<form method="POST" action="add-photo.php" enctype="multipart/form-data">
    <div class="form-group">
        <label for="form-control-file">Dodawanie zdjęcia</label>
        <input type="file" name="file" class="form-control-file" id="form-control-file" required>
    </div>
    <button class="btn btn-lg btn-primary" name="submit" type="submit">Dodaj zdjęcie</button>
</form>

提交表单时,我使用php代码:

if (isset($_POST['submit'])) {
$photo = new Photo();
$photo->setFile($_FILES['file']['tmp_name']);
$photo->setFileName($_FILES['file']['name']);

if ($photo->hasCorrectFileExtension() && $photo->hasCorrectSize()){
    $photo->uploadPhoto();
}
header('add-photo.php');
}

我创建了一个名为Photo的类,并具有hasCorrectFileExtension()hasCorrectSize()函数,用于在上传之前检查文件。

public function hasCorrectFileExtension()
    {
        if (in_array(pathinfo($this->fileName, PATHINFO_EXTENSION), $this->extensions)) {
            return true;
        } else {
            $_SESSION["errors"] = "Niepoprawne rozszerzenie przesyłanego pliku!";
            return false;
        }
    }

public function hasCorrectSize(){
        if (filesize($this->path . $this->fileName) < 100000000)
            return true;
        else
            $_SESSION["errors"] = "Zbyt duży rozmiar przesyłanego pliku!";
            return false;
    }

public function uploadPhoto()
    {
        move_uploaded_file($this->file, $this->path . $this->fileName);
        $_SESSION["success"] = "Plik został przesłany!";
    }

在HTML代码中,我创建了PHP脚本来打印警报或成功消息。

<?php
        if(isset($_SESSION["errors"])){
            echo '<div class="alert alert-danger" role="alert">';
            echo $_SESSION["errors"];
            echo '</div>';
            unset($_SESSION["errors"]);
        }
        else if (isset($_SESSION["success"])){
            echo '<div class="alert alert-success" role="alert">';
            echo 'Zdjęcie zostało wysłane!';
            echo '</div>';
            unset($_SESSION["success"]);
        }
        ?>

总结提交表单后的工作正常,并且警报/成功消息正确显示,但是此后,我将刷新页面,尽管我使用了unset($_SESSION["errors")/unset($_SESSION["success"])

,但仍然看到警报/成功消息

2 个答案:

答案 0 :(得分:1)

无论如何都不能尽快回复您的上一条消息,很抱歉-我重新创建了您的Photo类的近似值,以整理一个有效的演示来说明文件上传后消息的显示。在随后的页面重新加载中,该消息不再可用,并且文件也没有再次上传。

<?php

    session_start();

    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_FILES['file'] ) ){



        class Photo{
            /*********************************/
            /* approximation of actual Class */
            /*********************************/
            public function __construct(){
                $this->path=__DIR__ . '/images/';
                $this->extensions=['jpg','png','pjpeg','gif','webp'];
            }
            public function setFile($tmp){
                $this->file=$tmp;
            }
            public function setFileName($name){
                $this->fileName=$name;
            }
            public function hasCorrectFileExtension(){
                if( !in_array( pathinfo( $this->fileName, PATHINFO_EXTENSION), $this->extensions ) ) {
                    $_SESSION["errors"] = "Niepoprawne rozszerzenie przesyłanego pliku!";
                    return false;
                }
                return true;
            }
            public function hasCorrectSize(){
                if( filesize( $this->path . $this->fileName ) >= 100000000 ){
                    $_SESSION["errors"] = "Zbyt duży rozmiar przesyłanego pliku!";
                    return false;
                }
                return true;
            }           
            public function uploadPhoto(){
                move_uploaded_file($this->file, $this->path . $this->fileName);
                $_SESSION["success"] = "Plik został przesłany!";
            }
        }





        $photo = new Photo();
        $photo->setFile( $_FILES['file']['tmp_name'] );
        $photo->setFileName( $_FILES['file']['name'] );


        /*
            The various SESSION variables should be set by the
            following public methods.
        */
        if ( $photo->hasCorrectFileExtension() && $photo->hasCorrectSize() ){
            $photo->uploadPhoto();
        }



        # redirect to the same page to prevent accidental form re-submissions
        exit( header( sprintf( 'Location: %s', $_SERVER['SCRIPT_NAME'] ) ) );
    }
?>
<!DOCTYPE html />
<html>
    <head>
        <!-- head contents not shown -->
    </head>
    <body>

        <!-- HTML modified for clarity only -->
        <form method='POST' enctype='multipart/form-data'>
            <div>
                <label>Dodawanie zdjęcia
                    <input type='file' name='file' required />
                </label>
            </div>
            <button type='submit'>Dodaj zdjęcie</button>
        </form>


        <?php

            $svar='errors';
            if( !empty( $_SESSION[ $svar ] ) ){
                printf('
                    <div class="alert alert-danger" role="alert">%s</div>',
                    $_SESSION[ $svar ]
                );
                unset( $_SESSION[ $svar ] );
            }


            $svar='success';
            if( !empty( $_SESSION[ $svar ] ) ){
                printf('
                    <div class="alert alert-success" role="alert">%s</div>',
                    $_SESSION[ $svar ]
                );
                unset( $_SESSION[ $svar ] );
            }
        ?>
    </body>
</html>

答案 1 :(得分:0)

使用

session_unset()

就在Php关闭之前

<?php
    if(isset($_SESSION["errors"])){
        echo '<div class="alert alert-danger" role="alert">';
        echo $_SESSION["errors"];
        echo '</div>';
    }
    else if (isset($_SESSION["success"])){
        echo '<div class="alert alert-success" role="alert">';
        echo 'Zdjęcie zostało wysłane!';
        echo '</div>';
    }
session_unset();
    ?>

或者如果您需要重置整个会话变量,只需调用

session_destroy ();