当前位置:Gxlcms > PHP教程 > php实现简单的上传进度条,php上传进度条_PHP教程

php实现简单的上传进度条,php上传进度条_PHP教程

时间:2021-07-01 10:21:17 帮助过:46人阅读

php实现简单的上传进度条,php上传进度条


Web上传文件的三种解决方案分享给大家:

这里我要使用的是form法。通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数据以二进制编码的方式提交,在接收此请求的Servlet中用二进制流来获取内容,就可以取得上传文件的内容,从而实现文件的上传。

表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:

在网上找到了两种方式,PHP配合apc实现和利用uploadprogress实现,这次我要使用的是uploadprogress,点击地址可以下载到php相应版本的dll。安装php_uploadprogress.dll扩展,重启apache。

进度条实现原理:

这里用到了一个iframe无刷新上传文件的方法。

上传完成后的样子如图:

上面的HTML代码中要注意下UPLOAD_IDENTIFIER,这个是用来定位查看哪个文件的上传进度的。我这里就写死为一个1,大家可以写成一个php生成的随机数。下面是JS脚本:

  1. var proNum=0;
  2. var loop=0;
  3. var progressResult = "";
  4. var interval;
  5. function sendURL() {
  6. $.ajax({
  7. type : 'GET',
  8. url : "getprogress.php",
  9. async : true,
  10. cache : false,
  11. dataType : 'json',
  12. data: "progress_key=" + $('input[name=UPLOAD_IDENTIFIER]').val(),
  13. success : function(e) {
  14. proNum=parseInt(e);
  15. if(e){
  16. $('.barinner').css('width', proNum+"%");
  17. $('#showNum').html(proNum+"%");
  18. setTimeout("getProgress()", 200);
  19. }else{
  20. if(interval == 1){
  21. $('.barinner').css('width', "100%");
  22. $('#showNum').html("100%");
  23. }
  24. }
  25. }
  26. });
  27. }
  28. function getProgress(){
  29. loop++;
  30. sendURL();
  31. }
  32. function startProgress(){
  33. interval = 1;
  34. $('.barinner').css('width', proNum+"%");
  35. $('#showNum').html(proNum+"%");
  36. setTimeout("getProgress()", 500);
  37. }

下面是getprogress.php文件中的内容:

  1. <?php
  2. if (function_exists("uploadprogress_get_info")) {
  3. $info = uploadprogress_get_info($_GET['progress_key']);
  4. if(!empty($info)){
  5. if(($info['bytes_uploaded'] < $info['bytes_total']) && !empty($info['bytes_uploaded']) && !empty($info['bytes_total'])){
  6. $proNum = floor(($info['bytes_uploaded']/$info['bytes_total'])*100);
  7. }else{
  8. $proNum = 100;
  9. }
  10. echo $proNum;
  11. }else{
  12. echo 0;
  13. }
  14. }

在上传完成后,我展示了两种进度条的CSS,第二种是用最新的CSS3写的。用到了一些CSS3的动画属性。


  1. .prbar {
  2. margin:5px;
  3. width:500px;
  4. background-color:#dddddd;
  5. overflow:hidden;
  6. /* 边框效果 */
  7. border: 1px solid #bbbbbb;
  8. -moz-border-radius: 15px;
  9. border-radius: 15px;
  10. /* 为进度条增加阴影效果 */
  11. -webkit-box-shadow: 0px 2px 4px #555555;
  12. -moz-box-shadow: 0px 2px 4px #555555;
  13. box-shadow: 0px 2px 4px #555555;
  14. }
  15. /* No rounded corners for Opera, because the overflow:hidden dont work with rounded corners */
  16. doesnotexist:-o-prefocus, .prbar {
  17. border-radius:0px;
  18. }
  19. .prpos {
  20. width:0%;
  21. height:30px;
  22. background-color:#3399ff;
  23. border-right:1px solid #bbbbbb;
  24. /* CSS3 进度条渐变 */
  25. transition: width 2s ease;
  26. -webkit-transition: width 0s ease;
  27. -o-transition: width 0s ease;
  28. -moz-transition: width 0s ease;
  29. -ms-transition: width 0s ease;
  30. /* CSS3 Stripes */
  31. background-image: linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  32. background-image: -moz-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  33. background-image: -ms-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  34. background-image: -o-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  35. background-image: -webkit-gradient(linear, 100% 100%, 0 0,color-stop(.25, #99ccff), color-stop(.25, #3399ff),color-stop(.5, #3399ff),color-stop(.5, #99ccff),color-stop(.75, #99ccff),color-stop(.75, #3399ff),color-stop(1, #3399ff));
  36. background-image: -webkit-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  37. background-size: 40px 40px;
  38. /* Background stripes animation */
  39. animation: bganim 3s linear 2s infinite;
  40. -moz-animation: bganim 3s linear 2s infinite;
  41. -webkit-animation: bganim 3s linear 2s infinite;
  42. -o-animation: bganim 3s linear 2s infinite;
  43. -ms-animation: bganim 3s linear 2s infinite;
  44. }
  45. @keyframes bganim {
  46. from {background-position:0px;} to { background-position:40px;}
  47. }
  48. @-moz-keyframes bganim {
  49. from {background-position:0px;} to { background-position:40px;}
  50. }
  51. @-webkit-keyframes bganim {
  52. from {background-position:0px;} to { background-position:40px;}
  53. }
  54. @-o-keyframes bganim {
  55. from {background-position:0px;} to { background-position:40px;}
  56. }
  57. @-ms-keyframes bganim {
  58. from {background-position:0px;} to { background-position:40px;}
  59. }

以上就是本文的全部内容,希望对大家的学习有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1072547.htmlTechArticlephp实现简单的上传进度条,php上传进度条 Web上传文件的三种解决方案 分享给大家: 这里我要使用的是 form法 。通过为表单元素设置enctyp...

人气教程排行