我有一个问题,每次刷新浏览器时,我都希望包含 $_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>