我想用普通方法实现上传大文件,然后用Ajax结合APC来获取上传文件的总大小和当前已上传完的大小来制作上传的时度条。其中有关APC的代码在getprogress.php中,现在的问题是文件上传没问题,但进度条达不到效果,
大家帮忙看看
progress.php文件,直接的上传界面
<?php
//$id = md5(uniqid(rand(), true));
$id = uniqid("");
?>
<html>
<head><title>Upload Example</title></head>
<body>
<script language="javascript">
var xmlHttp;
var proNum=0;
var loop=0;
function createXHR(){
var xmlHttp;
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");//Microsoft.XMLHTTP
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
xmlHttp=createXHR();
function sendURL() {
var url="http://192.168.1.156/newUpTest/getprogress.php?progress_key=<?php echo $id;?>";
xmlHttp.onreadystatechange = doHttpReadyStateChange;
xmlHttp.open("GET",url,false);
xmlHttp.send("progress_key=<?php echo $id;?>");
}
function doHttpReadyStateChange() {
if (xmlHttp.readyState == 4){
proNum=parseInt(xmlHttp.responseText);
document.getElementById("progressinner").style.width = proNum+"%";
document.getElementById("showNum").innerHTML = proNum+"%";
if ( proNum < 100){
setTimeout("getProgress()", 100);
}
}
}
function getProgress(){
loop++;
document.getElementById("showNum2").innerHTML = loop;
sendURL();
}
function startProgress(){
document.getElementById("progressouter").style.display="block";
setTimeout("getProgress()", 100);
}
</script>
<iframe id="theframe" name="theframe"
src="upload.php?id=<?php echo($id); ?>"
style="border: none; height: 100px; width: 400px;" >
</iframe>
<br/><br/>
<div id="progressouter" style=
"width: 500px; height: 20px; border: 6px solid red; display:none;">
<div id="progressinner" style=
"position: relative; height: 20px; background-color: purple; width: 0%; ">
</div>
</div><div id='showNum'></div><br>
<div id='showNum2'></div>
</body>
</html>
upload.php 页面上传控件文件,内嵌到progress.php中
<?php
$id = $_GET['id'];
?>
<form enctype="multipart/form-data" id="upload_form"
action="target.php" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS"
id="progress_key" value="<?php echo $id?>"/>
<input type="file" id="test_file" name="test_file"/><br/>
<input onclick="window.parent.startProgress(); return true;"
type="submit" value="Upload!"/>
</form>
target.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST') {
move_uploaded_file($_FILES["test_file"]["tmp_name"],
"D:\WebSource\newUpTest\UploadTemp\" . $_FILES["test_file"]["name"]);
echo "<p>File uploaded. Thank you!</p>";
}
?>
getprogress.php 有关APC读取临时的上传文件大小的文件,此文件由progress.php里的Ajax代码调用
<?php
session_start();
if(isset($_GET['progress_key'])) {
$status = apc_fetch('upload_'.$_GET['progress_key']);
echo ($status['current']/$status['total'])*100;
}
?>
另附件有php_apc.dll 扩展库文件及相关的 php.ini 配置文件。
本人环境 : Windows XP+Apache2.2+PHP 5.2.3