2013-10-18 76 views
1

我試圖爲多個文件創建進度條,我上傳到服務器!我能夠上傳文件,但是當我嘗試獲取上傳進度信息時,會話數組爲空。請幫忙!請找出以下信息: 首先我主要的PHP網頁,我上傳的文件:

********************************************Main.php******************************** 
<?php session_start();?>//top of the page 
<div id = "centerMain" align="center"> 
      <iframe name="targetIframe" src="" style="width:50%;height:30%;position:relative;"></iframe> 
        <div id="addNewBlock" class="emboss blockPart" style="z-index:50000;padding:2%;position:relative;width:50%;left:25%;top:35%;border:1px solid black;"> 
        <form method="POST" action="handleItemAddition.php" target="targetIframe" enctype = "multipart/form-data" onSubmit="return checkAndStartUpload()"> 

<!--VERY IMPORTANT:the hidden element should come first before any element within the form--> <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" id="hidUpProg" value="upFilesInfo" />     
<script type ="text/javascript"> 


var upProgName = "<?php echo ini_get("session.upload_progress.name"); ?>"; 
       //alert(document.getElementById("hidUpProg").value); 
      </script> 
      <div class="stayLeft">Upload Photo:</div><input style="width:40%;" type="file" name = "itemImage[]" id="fileUp" class = "stayRight" multiple/><br/><br/> 


        <input type="button" id = "closeBut" style="width:20%;" value = "close" onclick="closeBlock()" class="utsaaBut stayLeft"/> 
        <input type="submit" id = "AddBut" style="width:20%;" value = "Done" class="utsaaBut stayRight"/>     
        </form> 

       </div> 
       </div> 
********************************************Main.php******************************** 

在下面找到它被調用的onsubmit =「checkAndStartUpload()」從上面

******************************************Javascript function**************************** 
function checkAndStartUpload() 
{ 
    var tmp = document.getElementById("fileUp"); 
    if(tmp.files.length == 0) 
    { 
     alert("No file selected. Kindly select a file"); 
     return false 
    } 

    $progressPtr = setInterval(handleProgress,50); 
    return true; 
} 

var it = 0; 
function handleProgress() 
{ 
    //alert("handleProgress"); 
    var xmlhttp; 
    if(window.XMLHttpRequest) 
    { 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else 
    { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     //alert("response"); 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      //alert("value:"+xmlhttp.responseText["content_length"]); 
      var res = xmlhttp.responseText; 
      alert("Response:"+res); 
      it++; 
      if(it == 25) 
      { 
       it = 0; 
      clearInterval($progressPtr); 
      } 
     } 
    } 

    xmlhttp.open("POST","handleProgressValue.php",true); 

    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 

    xmlhttp.send(upProgName+"=upFilesInfo"); 
} 

******************************************Javascript function******************* 

接下來的javscript功能是通過ajax返回會話值的文件

******************************************ajax progress return******************* 
<?php 

$key = ini_get("session.upload_progress.prefix") .$_POST[ini_get("session.upload_progress.name")]; 

if (!empty($_SESSION[$key])) { 
    $current = $_SESSION[$key]["bytes_processed"]; 
    $total = $_SESSION[$key]["content_length"]; 
    echo "Current".$current."$total".$total; 
    //echo $current < $total ? ceil($current/$total * 100) : 100; 
} 
else { 
    echo "100"; //IT ALWAYS RETURNS 100 MEANING$_SESSION[$key] IS ALWAYS EMPTY 
} 
/*session_start(); 
$key = ini_get("session.upload_progress.prefix").$_POST[ini_get("session.upload_progress.name")]; 
//var_dump($_SESSION[$key]); 
$tmp = $_SESSION[$key]; 
echo $tmp["bytes_processed"];*/ 

?> 
    ******************************************ajax progress return******************* 

最後handleItemAddition.php成功上傳圖片。 我也禁用了; session.upload_progress.cleanup =僅用於測試目的,因此如果上傳得到快速完成,它不應該清除值。

仍然我越來越空陣列。

+2

嘗試在每個頁面的頂部放置'session_start();'。 –

回答

0

嗨,對不起,我忘了更新。我在某個時候解決了這個問題。 我已經把session_start();除了handleProgressValue.php之外的所有文件的頂部,我已經評論它,而改變代碼後,我取消註釋它的工作。

但是,當我上傳28個文件說,爲什麼 計數($ _SESSION [$ key] [「files」]) 返回遞增值?它首先返回10,然後是17,24,28,然後$ _SESSION被取消設置