【发布时间】:2012-06-24 22:32:34
【问题描述】:
我正在尝试使用 PECL uploadprogress 扩展来实现一个非常基本的 AJAX 上传进度条。我找到了适用于所有浏览器的示例代码:http://svn.php.net/viewvc/pecl/uploadprogress/trunk/examples/。它使用 iframe 将更新写入。我想获取更新并做一些 jquery 来构建进度条。这是我的代码(我知道我没有写代码来说明上传结束的时间)client.php:
<?php
$id = md5(microtime() . rand());
?>
<!DOCTYPE html>
<html>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function getProgress(){
$.get("progress.php", {"ID":'<?php echo $id ?>'}, function(data){
console.log(data);
});
window.setTimeout(getProgress(), 5000);
}
</script>
<body>
<form onsubmit="getProgress()" target="_self" enctype="multipart/form-data" method="post">
<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" />
<label>Select File:</label>
<input type="file" name="file" />
<br/>
<label>Select File:</label>
<input type="file" name="file2" />
<br/>
<label>Upload File:</label>
<input id="submitButton" type="submit" value="Upload File" />
</form>
</body>
</html>
还有progress.php:
<?php
if (function_exists("uploadprogress_get_info")) {
$info = uploadprogress_get_info($_GET['ID']);
} else {
$info = false;
}
$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;
echo $progress;
我出错了,打印出来的都是 0。有什么想法吗?
【问题讨论】:
-
PHP 是作为 Apache 模块还是 cgi 运行? PECL 上传进度仅适用于 mod_php。
-
我已经确认使用 iframe 的演示代码确实有效。我正在尝试将其精简到最基本的要求。
标签: php jquery ajax upload progress