php上傳進度無效,$ _SESSION [$ key]為空

[英]php upload progress not working, $_SESSION[$key] is empty


I am trying to create a progress bar for multiple file i upload to a server!!! I am able to upload the files but when i try to fetch the upload progress information the session array is empty. Please do help!!! Please find below the information: Firstly my main php page where i upload the files:

我正在嘗試為我上傳到服務器的多個文件創建進度條!我能夠上傳文件,但是當我嘗試獲取上傳進度信息時,會話數組為空。請幫忙!!!請在下面找到相關信息:首先是我上傳文件的主要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********************************

find below the javscript function which gets called onSubmit = "checkAndStartUpload()" from above

在下面找到javscript函數,從上面調用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*******************

Next is the file which returns session value through ajax

接下來是通過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*******************

And finally handleItemAddition.php successfully uploads the images. I also disabled ;session.upload_progress.cleanup = On just for testing purpose so that if upload gets completed fast it should not clear the values.

最后,handleItemAddition.php成功上傳了圖片。我也禁用了; session.upload_progress.cleanup = On只是為了測試目的,所以如果上傳快速完成,它不應該清除值。

Still i am getting empty array .

我仍然得到空陣列。

1 个解决方案

#1


0  

Hey guys sorry i forgot to update. I fixed the issue sometime back. I had put session_start(); on top of all files but in handleProgressValue.php i had commented it while changing the code once i uncommented it worked.

嘿伙計們抱歉我忘了更新。我有時會修復這個問題。我把session_start();在所有文件的頂部,但在handleProgressValue.php我已經評論它,一旦我取消注釋它工作,更改代碼。

However when i am uploading 28 files say, why does count($_SESSION[$key]["files"]) return increasing values??? It first returned 10 then 17,24,28 and then $_SESSION got unset

但是,當我上傳28個文件時,為什么count($ _ SESSION [$ key] [“files”])會返回增加值???它首先返回10然后17,24,28然后$ _SESSION未設置


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/10/18/72f4f5213e0506ebd48f157c488a42ca.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com