0

我有一个问题,每次刷新浏览器时,我都希望包含 $_SESSION 变量的数组恢复为空白。现在假设我上传了 2 个文件,然后刷新浏览器,当我上传另一个文件时,它会显示以前上传的文件的名称,而不是它不应该上传的文件。如果刷新浏览器,如何让数组和会话变量恢复为空白?

下面是代码:

  function stopImageUpload(success){

          var imageNameArray = new Array();
// WHEN PAGE IS REFRESH, ARRAY SHOULD GO BACK TO BEING BLANK

 imageNameArray = <?php echo json_encode(isset($_FILES ['fileImage']['name']) ? $_FILES ['fileImage']['name'] : null); ?>;
//RETRIEVES THE SESSION VARIABLE FROM THE PHP SCRIPT OF THE FILE NAMES WHICH HAVE BEEN UPLOADED

          var result = '';



          if (success == 1){
             result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';

                for(var i=0;i<imageNameArray.length;i++)  //LOOP THROUGH ALL UPLOADED FILE NAMES
        {
             $('.listImage').append(imageNameArray[i]+ '<br/>');//APPEND FILE NAME

         }

          }
          else {
             result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
          }

    return true;

    }

下面是 php 脚本,它从上面的 javascript 函数上传另一个页面上的文件:

<?php

    session_start();

    $result = 0;
    $errors = array ();
    $dirImage = "ImageFiles/";


if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {

$fileName = $_FILES ['fileImage'] ['name'];

$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );


$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;

        if (count ( $errors ) == 0) {
            if (move_uploaded_file ( $fileTemp, $fileDst )) {
                $result = 1;


            }
        }

    }

    ?>

<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
4

3 回答 3

3

提交表单并且您的 PHP 完成工作后,您需要执行 303 重定向到您希望显示的页面。然后,当用户刷新页面时,您不会再次提交表单。

例如:

header('Location: ' . $_SERVER['PHP_SELF'], true, 303);
于 2012-04-27T17:19:39.130 回答
0

虽然我同意@Reza Sanaie 的评论,但您可以通过在成功上传后取消设置变量来解决您的特定问题:

     if (success == 1)
     {
         result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';

         for(var i=0;i<imageNameArray.length;i++)  //LOOP THROUGH ALL UPLOADED FILE NAMES
         {
             $('.listImage').append(imageNameArray[i]+ '<br/>');//APPEND FILE NAME

         }

         $_SESSION ['fileImage'] = NULL; // or use unset()
     }
于 2012-04-27T17:25:51.210 回答
0

只需在 PHP 中进行错误检查,然后将数组传递给 HTML 页面以显示页面中上传文件的列表。你不需要javascript。

看看如何在 PHP 中上传文件

于 2012-04-27T17:56:18.937 回答