2

我正在尝试为我上传到服务器的多个文件创建一个进度条!!!我可以上传文件,但是当我尝试获取上传进度信息时,会话数组为空。请帮忙!!!请在下面找到信息:首先是我上传文件的主 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()" 的 javscript 函数

******************************************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*******************

接下来是通过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 = On 只是为了测试目的,这样如果上传快速完成,它不应该清除这些值。

我仍然得到空数组。

4

1 回答 1

0

嘿伙计们对不起我忘了更新。我在某个时候解决了这个问题。我把 session_start(); 在所有文件之上,但在 handleProgressValue.php 中,我在更改代码时评论了它,一旦我取消注释它就起作用了。

但是,当我上传 28 个文件时,为什么 count($_SESSION[$key]["files"]) 返回增加的值???它首先返回 10,然后返回 17、24、28,然后 $_SESSION 未设置

于 2013-10-18T14:47:31.167 回答