PHP上传进度条深度解析
前言:Hi,我不是坦克,我是54chen,受哥学社动力所托,传递技术文章一篇,供大家吃晚饭时边吃边看,加强消化。
随着互联网的发展,越来越多的技术开始注重用户体验,以人为本才是长久之道,于是在上传的时候,大家都不再满足一个单一的“浏览”按钮,纷纷推出了带上传进度条的功能。而作为解释型语言的PHP,如何做到对上传文件的检测,如何实现上传进度条以其背后的原理,54chen将在本文中一步步展开。
一. 实现篇
一般情况,用PHP实现上传进度条就下面两种方法:
1.APC扩展(作者是PHP的创始人,5.2后PHP已经加入APC扩展)
2.PECL扩展模块 uploadprogress
不论是APC还是uploadprogress,都需要编译源码,因为原有的PHP函数根本不可能读取到临时文件夹里的东西。下面来看如何使用以及关键的代码:
1 2 3 4 5 6 7 8 9 10 | 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; } |
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 | 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)) { //上传成功 } } } 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 { //读取进度失败,另外处理逻辑 } } |
二. 原理篇
注意上一篇中的红色函数。
下载到uploadprogress1.0.1进行源码分析,在代码中作了注释。
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 42 | static void uploadprogress_file_php_get_info(char * id, zval * return_value) { char s[1024]; char * filename; char * template; FILE *F; TSRMLS_FETCH(); template = INI_STR("uploadprogress.file.filename_template"); //这里读取设置好的模板 if (strcmp(template, "") == 0) { return; } else { filename = uploadprogress_mk_filename( id, template );//存在的话,会创建 if (!filename) return; F = VCWD_FOPEN(filename, "rb"); if (F) { array_init(return_value); while ( fgets(s, 1000, F) ) {//从流中读取一字符串 *s结果数据的首地址;1000-1:一次读入数据块的长度,其默认值为1k,即1024;F文件指针 char *k, *v, *e; int index = 0; e = strchr(s,'='); //查找字符串s中首次出现字符=的位置 if (!e) continue; *e = 0; /* break the line into 2 parts */ v = e+1; k = s; /* trim spaces in front of the name/value */ while (*k && *k <= 32) k++; while (*v && *v <= 32) v++; /* trim spaces everywhere in the name */ for (e=k; *e; e++) if (*e <= 32) { *e = 0; break; } /* trim spaces only at the end of the value */ /* http://pecl.php.net/bugs/bug.php?id=14525 */ //for (e=v; *e; e++) if (*e <= 32) { *e = 0; break; } if (v != NULL) { //当文件有内容时 for (index = strlen(v); index > 0; index--) { if (v[index] > 32) break; //累计 v[index] = 0; } } add_assoc_string( return_value, k, v, 1 ); } fclose(F); } } if (filename) efree(filename); return; } |
在源码中还能发现:
1 2 3 4 5 6 | PHP_MINIT_FUNCTION(uploadprogress) { REGISTER_INI_ENTRIES(); php_rfc1867_callback = uploadprogress_php_rfc1867_file; return SUCCESS; } |
在MINIT中修改了php_rfc1867_callback,抽取uploadprogress_php_rfc1867_file的关键代码:
1 2 3 4 5 6 7 8 9 10 11 12 | upload_id = emalloc(strlen(*e_data->value) + 1); strcpy(upload_id, *e_data->value); progress->upload_id = upload_id; progress->time_last = time(NULL); progress->speed_average = 0; progress->speed_last = 0; progress->bytes_uploaded = read_bytes; progress->files_uploaded = 0; progress->est_sec = 0; progress->identifier = uploadprogress_mk_filename(upload_id, template);//在指定的模板位置放下了临时文件 progress->identifier_tmp = emalloc(strlen( progress->identifier) + 4); sprintf( progress->identifier_tmp, "%s.wr", progress->identifier ); |
关于php_rfc1867_callback是什么,可以看哥学社另一哥鸟哥的分析 http://www.laruence.com/2008/11/07/586.html
三.总结
在探测临时文件大小的时候,APC和 uploadprogress其实是大同小异的方法,先记录,再取大小百分比。
关于哥学社:
哥学社是一个自由博客组织,以提高原创技术博客质量,为成员博客增加更有效评论,由来自腾讯、百度、盛大、人人网、新浪、奇虎、TOM等各大企业的哥们共同起草发起。
关于五四陈:
54chen(陈臻),人人网分布式存储研究人员,业余时间混迹于各技术组织且乐此不疲。目前关注实施PHP培训。对flex等前端技术有一点研究。
个人技术站点:http://www.54chen.com/ 。可以通过电子邮件 czhttp@gmail.com 联系到他。
本文提及的文章:
http://www.php.net/releases/5_2_0.php
http://www.ibm.com/developerworks/cn/opensource/os-php-v525/
6 Comments to “ PHP上传进度条深度解析 ”
Post comment
分类目录
- ActionScript (2)
- CSS (25)
- Java (3)
- JavaScript (41)
- PHP (108)
- 心情杂谈 (34)
- 收集整理 (77)
- 本站原创 (57)
最近文章
- 在sae中利用SaeFetchurl进行豆瓣的OAuth授权
- OAuth 1.0a与1.0协议的改进…
- 深入理解OAuth与豆瓣OAuth test
- include_path+__autoload与数组+__autoload的比较
- 将google ssl设置为IE8的默认搜索引擎..
- 我们来做一个会呼吸的菜单吧!!
- 在编译php-fpm0.6的时候需要注意的一些问题
- 使用PHP将大文件导入到数据库中..
- 关于用PHP调用WebService中参数为complexType的问题
- 神奇的两次按位非运算符
- 百路推免费短网址服务..首创”收藏夹获取短网址”..
- 哥学社正式上线..
- jQuery中getJSON跨域原理详解
- Web辅助工具条(原名:河蟹工具条CrabBar)0.1发布
- 腾讯微博PC端发图教程
近期评论
- 老飞的小窝 在 jQuery中getJSON跨域原理详解 上的评论
- Jackie.Hamos 在 将google ssl设置为IE8的默认搜索引擎.. 上的评论
- Elmer Zhang 在 在sae中利用SaeFetchurl进行豆瓣的OAuth授权 上的评论
- 茶叶蛋 在 一个PHP+AJAX留言板的完整例子.非常简单! 上的评论
- 9527 在 我们为什么要抛弃模板引擎?? 上的评论
- hikurasai 在 Flash TagCloud中文版. 上的评论
- lx 在 PHP上传进度条深度解析 上的评论
- lx 在 使用PHP将大文件导入到数据库中.. 上的评论
- nflauthentic 在 哥学社正式上线.. 上的评论
- uggsale 在 有三个主题的CSS导航菜单 上的评论
文章归档
- 2010 年八月 (4)
- 2010 年七月 (3)
- 2010 年六月 (4)
- 2010 年五月 (2)
- 2010 年四月 (9)
- 2010 年三月 (12)
- 2010 年二月 (1)
- 2010 年一月 (3)
- 2009 年十二月 (2)
- 2009 年十一月 (3)
- 2009 年十月 (3)
- 2009 年九月 (5)
- 2009 年八月 (4)
- 2009 年七月 (6)
- 2009 年六月 (8)
- 2009 年五月 (8)
- 2009 年四月 (16)
- 2009 年三月 (19)
- 2009 年二月 (22)
- 2009 年一月 (20)
- 2008 年十二月 (38)
- 2008 年十一月 (22)
- 2008 年十月 (7)
- 2008 年九月 (3)
- 2008 年八月 (24)


本文同时发在了
http://www.54chen.com/cc/php-upload-progress.html
在两边评论,哥都同时会关注。
陈老大 我给你重新排版了下 加了高亮了..
请教一下,swfupload的上传获取进度条是怎么实现的呢?
swfupload不能获取单独一张图片的上传进度,它是计算n张图片的进度的,完成一张进度就是1/n
哥理解错了。
swfupload有2个亮点
1.多文件选择
2.文件上传进度显示
进度是本地上传的速度。也就是文件从网卡发送出去的速度。
恩,都是哥,好多哥 :D