国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP文件上傳帶進度條

2024-09-04 11:45:06
字體:
來源:轉載
供稿:網友

實現篇:

一般情況,用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;//開源代碼Vevb.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

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. elseif (!emptyempty($_get['sessid'])) { 
  12. header(”expires: mon, 26 jul 1997 05:00:00 gmt”); 
  13. header(”last-modified: ” . gmdate(”d, d m y h:i:s”) . ” gmt”); 
  14. header(”cache-control: no-store, no-cache, must-revalidate”); 
  15. header(”cache-control: post-check=0, pre-check=0′, false); 
  16. header(”pragma: no-cache”); 
  17. header(”content-type:text/html;charset=utf-8′); 
  18. $unique_id = $_get['sessid']; 
  19. $uploadvalues = uploadprogress_get_info($unique_id); 
  20. if (is_array($uploadvalues)) { 
  21. echo json_encode($uploadvalues); 
  22. else { 
  23. //讀取進度失敗,另外處理邏輯 

二.原理篇

注意上一篇中的紅色函數,下載到uploadprogress1.0.1進行源碼分析,在代碼中作了注釋,代碼如下:

  1. static void uploadprogress_file_php_get_info(char * id, zval * return_value) 
  2. char s[1024]; 
  3. char * filename; 
  4. char * template; 
  5. file *f; 
  6. tsrmls_fetch(); 
  7. template = ini_str(”uploadprogress.file.filename_template”); <<這里讀取設置好的模板 
  8. if (strcmp(template, “”) == 0)  { 
  9. return
  10. else { 
  11. filename = uploadprogress_mk_filename( id, template );<<<存在的話,會創建 
  12. if (!filename) return
  13. f = vcwd_fopen(filename, “rb”); 
  14. if (f) { 
  15. array_init(return_value); 
  16. while ( fgets(s, 1000, f) ) {<<<從流中讀取一字符串 *s結果數據的首地址;1000-1:一次讀入數據塊的長度,其默認值為1k,即1024;f文件指針 
  17. char *k, *v, *e; 
  18. int index = 0; 
  19. e = strchr(s,’='); <<<查找字符串s中首次出現字符=的位置 
  20. if (!e) continue
  21. *e = 0; /* break the line into 2 parts */ 
  22. v = e+1; 
  23. k = s; 
  24. /* trim spaces in front of the name/value */ 
  25. while (*k && *k <= 32) k++; 
  26. while (*v && *v <= 32) v++; 
  27. /* trim spaces everywhere in the name */ 
  28. for (e=k; *e; e++) if (*e <= 32) { *e = 0; break; } 
  29. /* trim spaces only at the end of the value */ 
  30. /* http://111cn.net */ 
  31. //for (e=v; *e; e++) if (*e <= 32) { *e = 0; break; } 
  32. if (v != null) {<<<當文件有內容時 
  33. for (index = strlen(v); index > 0; index–) { 
  34. if (v[index] > 32) break;<<<累計 
  35. v[index] = 0; 
  36. add_assoc_string( return_value, k, v, 1 ); 
  37. fclose(f); 
  38. if (filename) efree(filename); 
  39. return

在源碼中還能發現如下代碼:

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

在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 );

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 赣榆县| 巴青县| 读书| 泉州市| 泗阳县| 沧州市| 昭平县| 内黄县| 广昌县| 绥德县| 南京市| 万州区| 舞钢市| 兴山县| 阳曲县| 离岛区| 宁化县| 大埔区| 防城港市| 诸暨市| 石楼县| 中江县| 鞍山市| 阿坝县| 保靖县| 广平县| 淳安县| 利津县| 德化县| 健康| 南木林县| 阿勒泰市| 临汾市| 白沙| 陕西省| 平泉县| 喀喇沁旗| 丰县| 平遥县| 彰化市| 嘉定区|