一种是使用APC扩展模块:http://pecl.php.net/package/apc。
另外一种是使用PECL扩展模块 uploadprogress实现:http://pecl.php.net/package/uploadprogress。
1、APC实现方法:
安装APC,参照官方文档安装,可以使用PECL模块安装。
配置php.ini,设置参数 apc.rfc1867=1 ,使APC支持上传进度条功能,
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php /** APC方法 上传进度条 */ if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) { //上传请求 $status = apc_fetch( 'upload_' . $_POST[ 'APC_UPLOAD_PROGRESS' ]); $status[ 'done' ] = 1; echo json_encode($status); //输出给用户端页面里的ajax调用,相关文档请自己寻找 exit; } elseif (isset($_GET[ 'progress_key' ])) { //读取上传进度 $status = apc_fetch( 'upload_' .$_GET[ 'progress_key' ]); echo json_encode($status); exit; } else { //其他代码,比如上传<a href="http://www.jb200.com/zt/biaodan/" target="_blank" class="infotextkey">表单</a>等 } |
2、uploadprogress 模块实现方法:
使用PECL模块安装方法安装该模块
php.ini里面设置 uploadprogress.file.filename_template = “/tmp/upd_%s.txt”
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php /** 上传进度条 www.jb200.com */ if ($_SERVER[ 'REQUEST_METHOD' ]== 'POST' ) { if (is_uploaded_file($_FILES[ 'upfile' ][ 'tmp_name' ])) { $upload_dir = 'your_path/' ; $ext = strrchr($_FILES[ 'video' ][ 'name' ], '.' ); $sessid = $_POST[ 'UPLOAD_IDENTIFIER' ] ; $tmpfile = $upload_dir . $sessid; $sessfile = $upload_dir . $sessid .$ext; if (move_uploaded_file($_FILES[ 'upfile' ][ 'tmp_name' ],$tmpfile)) { //上传成功 } else { //上传失败 } else { //上传错误 } elseif (!empty($_GET[ 'sessid' ])) { header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" ); header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false ); header( "Pragma: no-cache" ); header( "Content-Type:text/html;charset=UTF-8" ); $unique_id = $_GET[ 'sessid' ]; $uploadvalues = uploadprogress_get_info($unique_id); if (is_array($uploadvalues)) { echo json_encode($uploadvalues); } else { //读取进度失败,另外处理逻辑 } } else { //显示上传表单 } ?> |
有上传进度条的代码,看起来比较直观,用户体验好。