PHP文件上传带进度条

实现篇:

一般情况,用php实现上传进度条就下面两种方法:

1.apc扩展(作者是php教程的创始人,5.2后php已经加入apc扩展)

2.pecl扩展模块 uploadprogress

不论是apc还是uploadprogress,都需要编译源码,因为原有的php函数根本不可能读取到临时文件夹里的东西,下面来看如何使用以及关键的代码:apc实现方法:

1.安装apc

2.配置php.ini,设置参数 apc.rfc1867=1

3.关键代码:

  1. if ($_server['request_method'] == ‘post’) { //上传请求
  2. $status = apc_fetch(’upload_’ . $_post['apc_upload_progress']);
  3. $status['done'] = 1;//开源代码phpfensi.com
  4. echo json_encode($status); //输出给用户端页面里的ajax调用,相关文档请自己寻找
  5. exit;
  6. } elseif (isset($_get['progress_key'])) { //读取上传进度
  7. $status = apc_fetch(’upload_’.$_get['progress_key']);
  8. echo json_encode($status);
  9. exit;
  10. }

uploadprogress实现方法:

1.使用pecl 安装uploadprogress

2.php.ini里面设置 uploadprogress.file.filename_template = “/tmp/upd_%s.txt”

3.关键代码如下:

  1. if($_server['request_method']==’post’) {
  2. if (is_uploaded_file($_files['upfile']['tmp_name'])) {
  3. $upload_dir = ‘your_path/’;
  4. $ext = strrchr($_files['video']['name'], ‘.’);
  5. $sessid = $_post['upload_identifier'] ;
  6. $tmpfile = $upload_dir . $sessid;
  7. $sessfile = $upload_dir . $sessid .$ext;
  8. if (move_uploaded_file($_files['upfile']['tmp_name'],$tmpfile)) {
  9. //上传成功
  10. }
  11. }
  12. } elseif (!emptyempty($_get['sessid'])) {
  13. header(”expires: mon, 26 jul 1997 05:00:00 gmt”);
  14. header(”last-modified: ” . gmdate(”d, d m y h:i:s”) . ” gmt”);
  15. header(”cache-control: no-store, no-cache, must-revalidate”);
  16. header(”cache-control: post-check=0, pre-check=0′, false);
  17. header(”pragma: no-cache”);
  18. header(”content-type:text/html;charset=utf-8′);
  19. $unique_id = $_get['sessid'];
  20. $uploadvalues = uploadprogress_get_info($unique_id);
  21. if (is_array($uploadvalues)) {
  22. echo json_encode($uploadvalues);
  23. } else {
  24. //读取进度失败,另外处理逻辑
  25. }
  26. }

二.原理篇

注意上一篇中的红色函数,下载到uploadprogress1.0.1进行源码分析,在代码中作了注释,代码如下:

  1. static void uploadprogress_file_php_get_info(char * id, zval * return_value)
  2. {
  3. char s[1024];
  4. char * filename;
  5. char * template;
  6. file *f;
  7. tsrmls_fetch();
  8. template = ini_str(”uploadprogress.file.filename_template”); <<这里读取设置好的模板
  9. if (strcmp(template, “”) == 0) {
  10. return;
  11. } else {
  12. filename = uploadprogress_mk_filename( id, template );<<<存在的话,会创建
  13. if (!filename) return;
  14. f = vcwd_fopen(filename, “rb”);
  15. if (f) {
  16. array_init(return_value);
  17. while ( fgets(s, 1000, f) ) {<<<从流中读取一字符串 *s结果数据的首地址;1000-1:一次读入数据块的长度,其默认值为1k,即1024;f文件指针
  18. char *k, *v, *e;
  19. int index = 0;
  20. e = strchr(s,’='); <<<查找字符串s中首次出现字符=的位置
  21. if (!e) continue;
  22. *e = 0; /* break the line into 2 parts */
  23. v = e+1;
  24. k = s;
  25. /* trim spaces in front of the name/value */
  26. while (*k && *k <= 32) k++;
  27. while (*v && *v <= 32) v++;
  28. /* trim spaces everywhere in the name */
  29. for (e=k; *e; e++) if (*e <= 32) { *e = 0; break; }
  30. /* trim spaces only at the end of the value */
  31. /* http://111cn.net */
  32. //for (e=v; *e; e++) if (*e <= 32) { *e = 0; break; }
  33. if (v != null) {<<<当文件有内容时
  34. for (index = strlen(v); index > 0; index–) {
  35. if (v[index] > 32) break;<<<累计
  36. v[index] = 0;
  37. }
  38. }
  39. add_assoc_string( return_value, k, v, 1 );
  40. }
  41. fclose(f);
  42. }
  43. if (filename) efree(filename);
  44. return;
  45. }
  46. }

在源码中还能发现如下代码:

  1. php_minit_function(uploadprogress)
  2. {
  3. register_ini_entries();
  4. php_rfc1867_callback = uploadprogress_php_rfc1867_file;
  5. return success;
  6. }

在minit中修改了php_rfc1867_callback,抽取uploadprogress_php_rfc1867_file的关键代码,代码如下:

  1. upload_id = emalloc(strlen(*e_data->value) + 1);
  2. strcpy(upload_id, *e_data->value);
  3. progress->upload_id = upload_id;
  4. progress->time_last = time(null);
  5. progress->speed_average = 0;
  6. progress->speed_last = 0;
  7. progress->bytes_uploaded = read_bytes;
  8. progress->files_uploaded = 0;
  9. progress->est_sec = 0;
  10. progress->identifier = uploadprogress_mk_filename(upload_id, template);<<<在指定的模板位置放下了临时文件
  11. progress->identifier_tmp = emalloc(strlen( progress->identifier) + 4);
  12. sprintf( progress->identifier_tmp, “%s.wr”, progress->identifier );