1.用apc实现
这是一个完整可用的代码,部分代码来自于网络。
PHP:5.26
使用步骤:
开启APC.
extension=php_apc.dll
apc.rfc1867 = On
代码:
<?php
//<BS>X_REQUESTED_WITH
/* 上传文件 */
if(isset($_FILES['upfile'])){
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/uploadprogress/upfile/";
$uploaddir.= date("YmdHis",time()).'_'.$_FILES['upfile']['name'];
if(move_uploaded_file($_FILES["upfile"]["tmp_name"], $uploaddir))
{
echo "上传成功!";
exit;
}
}
/* 获取上传进度信息 */
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
if(isset($_GET['APC_UPLOAD_PROGRESS']) && $_GET['do'] == 'retrieving') {
$status = apc_fetch('upload_'.$_GET['APC_UPLOAD_PROGRESS']);
if($status['total']!=0 && !empty($status['total'])) {
$json = array(
'per'=> $status['current']/$status['total']*100,
'total'=> round($status['total']/1024),
'current'=> round($status['current']/1024),
);
echo json_encode($json);
exit;
}
else {
echo (0);
exit;
}
}
}
?>
<mce:script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" mce_src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"><!--
</script >
<script type="text/javascript" src="jquery.form.js" mce_src="jquery.form.js"></script >
<script type="text/javascript">
$(document).ready(function() {
// This is more like it!
$('#upload_form').ajaxForm(function() {
//alert("Thank you for your comment!");
});
});
// --></mce:script>
<mce:script type="text/javascript" ><!--
function getProgress(upid){
var url = "index.php";
$.getJSON(
url,
{ APC_UPLOAD_PROGRESS: upid,do:'retrieving'},
function(json){
$("#progressinner").width(json.per+"%");
$("#progressinner").html(parseInt(json.per)+"%");
$("#upstatus").html('文件大小:'+json.total+'KB'+' 已上传:'+json.current+'KB');
if (json.per < 100){
setTimeout(function(){
getProgress(upid);
}, 10);
}else{
$("#upstatus").html("文件上传完成");
$("#progressouter").hide("slow");
}
}
)
}
function startProgress(){
var upid = $("#progress_key").val();
$("#progressinner").html("0%");
$("#progressinner").width("0%");
$("#progressouter").show();
setTimeout(function(){
getProgress(upid);
}, 100);
}
// --></mce:script>
<form enctype="multipart/form-data" id="upload_form" action="index.php" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo uniqid(mt_srand())?>"/>
<input type="file" id="upfile" name="upfile"/><br/><br/>
<input type="submit" id="filesubmit" value="上传" οnclick="startProgress();"/>
</form>
<div id="upstatus" style="width: 500px; height: 30px; border: 1px solid #ffffde; color:#796140;">
</div
<div id="progressouter" style="width:500px;height:20px;border:3px solid #de7e00;display:none;">
<div id="progressinner" style="position:relative;height:20px;color:#796140;background-color:#f6d095;width: 0%;">
</div>
</div>
2.php扩展uploadprogress
- session.upload_progress.enabled[=1] : 是否启用上传进度报告(默认开启)
- session.upload_progress.cleanup[=1] : 是否在上传完成后及时删除进度数据(默认开启, 推荐开启).
- session.upload_progress.prefix[=upload_progress_] : 进度数据将存储在_SESSION[session.upload_progress.prefix . _POST[session.upload_progress.name]]
- session.upload_progress.name[=PHP_SESSION_UPLOAD_PROGRESS] : 如果_POST[session.upload_progress.name]没有被设置, 则不会报告进度.
- session.upload_progress.freq[=1%] : 更新进度的频率(已经处理的字节数), 也支持百分比表示’%’.
- session.upload_progress.min_freq[=1.0] : 更新进度的时间间隔(秒级)
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden"
name="<?php echo ini_get("session.upload_progress.name"); ?>" value="laruence" />
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="submit" />
</form>
如果我们上传一个足够大的文件(网速要是足够慢就更好:P), 我们就可以从_SESSION中, 得到类似下面的进度信息:
$_SESSION["upload_progress_laruence"] = array(
"start_time" => 1234567890, // 请求时间
"content_length" => 57343257, // 上传文件总大小
"bytes_processed" => 453489, // 已经处理的大小
"done" => false, // 当所有上传处理完成后为TRUE
"files" => array(
0 => array(
"field_name" => "file1", // 表单中上传框的名字
// The following 3 elements equals those in $_FILES
"name" => "foo.avi",
"tmp_name" => "/tmp/phpxxxxxx",
"error" => 0,
"done" => true, // 当这个文件处理完成后会变成TRUE
"start_time" => 1234567890, // 这个文件开始处理时间
"bytes_processed" => 57343250, // 这个文件已经处理的大小
),
// An other file, not finished uploading, in the same request
1 => array(
"field_name" => "file2",
"name" => "bar.avi",
"tmp_name" => NULL,
"error" => 0,
"done" => false,
"start_time" => 1234567899,
"bytes_processed" => 54554,
),
)
);