php/apc 监控文件上传进度

原文地址: http://blog.csdn.net/lmss82/archive/2010/05/10/5574772.aspx

 

这是一个完整可用的代码,部分代码来自于网络。

 

PHP:

5.26

 

JS环境:

jquery.js,jquery_form.js

 

使用步骤:

 

开启APC.

 

下载php_apc.dll,修改php.ini文件增加以下内容:


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="上传" onclick="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>

 

你可能感兴趣的:(json,function,server,upload,XMLhttpREquest,extension)