2

我在下面有一个 javascript 函数,当文件完成上传时,它将显示必要的消息:

function stopImageUpload(success){

      var imagename = <?php echo json_encode($imagename); ?>;   
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
         $('.listImage').append(imagename + '<br/>');
      }
      else {
         result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
      }

return true;
}

现在我正在尝试检索下面的 $_SESSION 变量,然后使用上面的 javascript 函数(位于单独的页面上)将 $_SESSION 称为“$imagename”,然后将其附加到 .list 中。

但我的问题是如何编写上面的 if issett 语句,以便它可以判断下面的 $_SESSION 变量是否已设置,然后我可以使用 $imagename 来检索会话?

下面是包含 $_SESSION 和上传文件的 php 脚本:

    if (isset($_POST['fileImage'])) {
      $_SESSION['imagename'] =  $_FILES['fileImage']['name'];

    if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {  
        $_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];
        $parts = explode(".",$_FILES['fileImage']['name']);
        $ext = array_pop($parts);
        $base = implode(".",$parts);
        $n = 2;

        while( file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
        $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

        move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
        "ImageFiles/" . $_FILES["fileImage"]["name"]);
        $result = 1;


    }
        else
          {
          move_uploaded_file($_FILES["fileImage"]["tmp_name"],
          "ImageFiles/" . $_FILES["fileImage"]["name"]);
          $result = 1;

          }
      }


    ?>

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

1 回答 1

0

尝试

var imagename = <?php if (isset($_SESSION['imagename'])) echo json_encode($_SESSION['imagename']); ?>;
于 2012-04-21T21:17:08.503 回答