你将如何在PHP 5.4中实现基本的文件上传进度条?

问题描述:

我注意到这是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

<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