2013-03-07 93 views
5

我注意到這是PHP中增加了PHP 5.4的改進。你將如何在PHP 5.4中實現基本的文件上傳進度條?

我也明白這使用SESSION狀態,並且爲了顯示您需要某種Javscript來更新HTML。我相信JavaScript是通過按提交按鈕觸發的。並且可以設置間隔計時器來檢查進度。

我想要做的就是讓進度更新div的HTML。 (例如,25%... 35%)

我的理解迄今:
- 你需要有一個隱藏的價值
一種形式 - 這從在
信息該名稱創建一個會話變量 - 你需要觀察進度以獲得其狀態

選項
- 方法1:使用可以在線查找的預建的程序。很少/沒有解釋它。
- 方法2:將文件發送到另一個php文件並使用JSinterval檢查其進度。
- 方法3:是否有辦法在同一頁面上完成所有操作?

+0

根據定義,文件上傳進度條根本就不是什麼東西。 – 2013-03-07 15:09:45

+0

你有什麼嘗試?你看過http://php.net/manual/en/session.upload-progress.php嗎? – 2013-03-07 15:11:36

+1

我對這個過程感到困惑。你在強制Javascript提交表單後獲得狀態嗎?或者通過使用onclick事件,上傳開始,你只是得到它的地位? – 2013-03-07 15:15:29

回答

0
<div id="progress"></div> 
<form action="/upload.php" method="POST" enctype="multipart/form-data" id="upload"> 
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="upload" /> 

<input type="file" name="file" id="file" /> 
<input type="submit" value="Upload" onclick="upload()"/> 
</form> 

    <script> 
     //Check Sumbit has been clicked; either onclick or event handler 
     //Setup an interval timer updating the 'PROGRESS' div each check 
     //Each Interval: Get SESSION VARIABLE status and Update the div 'PROGRESS' 
     //Once file has completed; Do we redirect or submit the form? 

    function upload() 
    { 
    var xmlhttp; 
    if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } 
    else 
     {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    xmlhttp.onreadystatechange=function() 
     { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
     document.getElementById("progress").innerHTML=setInterval(function(){xmlhttp.responseText},500); 
     } 
     } 
    xmlhttp.open("POST","upload_progress.php",true); 
    xmlhttp.send(); 

    } 

</script> 
+0

這項工作? – 2013-03-07 16:12:48

+0

小心你的一些小錯誤..例如setInterval部分上的「==」... – 2013-03-07 17:09:38

+0

謝謝 - 我會編輯它。 那麼如何進一步呢?就像上傳完成後刷新頁面一樣? – 2013-03-07 18:05:50