Rumah > pembangunan bahagian belakang > tutorial php > php上传进度条详细解读(推荐)

php上传进度条详细解读(推荐)

墨辰丷
Lepaskan: 2023-03-29 21:42:01
asal
2649 orang telah melayarinya

本篇文章主要介绍php上传进度条详细解读(推荐),感兴趣的朋友参考下,希望对大家有所帮助。

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

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

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

 

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

进度条实现原理:

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

上传完成后的样子如图:

<body>
  <p style="padding:20px">
   <form action="action.php" enctype="multipart/form-data" method="post" target="iframeUpload">
    <iframe name="iframeUpload" width="400" height="400" frameborder=&#39;1&#39;></iframe>
    <input type="hidden" name="UPLOAD_IDENTIFIER" value="1" />
    <input id="file1" name="file1" type="file"/> 
    <input value="上传" type="submit" onclick="startProgress()"/> 
   </form>
  </p>
  <p style="width: 500px; height: 20px;border:1px solid red"> 
   <p style="position: relative; height: 20px; background-color: purple; width: 0%;" class="barinner"></p>
  </p>
  <p id=&#39;showNum&#39;></p>
  <p class="prbar">
   <p class="prpos barinner"></p>
  </p>
 </body>
Salin selepas log masuk

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

 var proNum=0; 
  var loop=0; 
  var progressResult = ""; 
  var interval; 
  function sendURL() { 
   $.ajax({ 
    type : &#39;GET&#39;, 
    url : "getprogress.php", 
    async : true, 
    cache : false, 
    dataType : &#39;json&#39;,
    data: "progress_key=" + $(&#39;input[name=UPLOAD_IDENTIFIER]&#39;).val(),
    success : function(e) { 
     proNum=parseInt(e); 
     if(e){ 
      $(&#39;.barinner&#39;).css(&#39;width&#39;, proNum+"%");
      $(&#39;#showNum&#39;).html(proNum+"%");
      setTimeout("getProgress()", 200); 
   
     }else{ 
      if(interval == 1){ 
       $(&#39;.barinner&#39;).css(&#39;width&#39;, "100%");
       $(&#39;#showNum&#39;).html("100%");
      } 
     }
    } 
   }); 
  } 
  function getProgress(){ 
   loop++; 
   sendURL(); 
  } 
  function startProgress(){ 
   interval = 1;
   $(&#39;.barinner&#39;).css(&#39;width&#39;, proNum+"%"); 
   $(&#39;#showNum&#39;).html(proNum+"%");
  setTimeout("getProgress()", 500); 
  }
Salin selepas log masuk

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

 <?php 
 if (function_exists("uploadprogress_get_info")) {
  $info = uploadprogress_get_info($_GET[&#39;progress_key&#39;]);
  if(!empty($info)){
   if(($info[&#39;bytes_uploaded&#39;] < $info[&#39;bytes_total&#39;]) && !empty($info[&#39;bytes_uploaded&#39;]) && !empty($info[&#39;bytes_total&#39;])){
    $proNum = floor(($info[&#39;bytes_uploaded&#39;]/$info[&#39;bytes_total&#39;])*100);  
   }else{
    $proNum = 100;
   }
   echo $proNum;
  }else{
   echo 0;
  }
 }
Salin selepas log masuk

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

 .prbar {
  margin:5px;
  width:500px;
  background-color:#dddddd;
  overflow:hidden;
  
  /* 边框效果 */
  border: 1px solid #bbbbbb;
  -moz-border-radius: 15px;
  border-radius: 15px;
    
  /* 为进度条增加阴影效果 */
  -webkit-box-shadow: 0px 2px 4px #555555;
  -moz-box-shadow: 0px 2px 4px #555555;
  box-shadow: 0px 2px 4px #555555;   
 }
 /* No rounded corners for Opera, because the overflow:hidden dont work with rounded corners */
 doesnotexist:-o-prefocus, .prbar {
 border-radius:0px;
 }
 .prpos {
  width:0%;
  height:30px;
  background-color:#3399ff;
  border-right:1px solid #bbbbbb;
  /* CSS3 进度条渐变 */
  transition: width 2s ease;
  -webkit-transition: width 0s ease;
  -o-transition: width 0s ease;
  -moz-transition: width 0s ease;
  -ms-transition: width 0s ease;
  /* CSS3 Stripes */
  background-image: linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  background-image: -moz-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  background-image: -ms-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  background-image: -o-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  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));
  background-image: -webkit-linear-gradient(135deg,#3399ff 25%,#99ccff 25%,#99ccff 50%, #3399ff 50%, #3399ff 75%,#99ccff 75%,#99ccff 100%);
  background-size: 40px 40px;
  /* Background stripes animation */
  animation: bganim 3s linear 2s infinite;
  -moz-animation: bganim 3s linear 2s infinite;
  -webkit-animation: bganim 3s linear 2s infinite;
  -o-animation: bganim 3s linear 2s infinite;
  -ms-animation: bganim 3s linear 2s infinite;
 }
 @keyframes bganim {
  from {background-position:0px;} to { background-position:40px;}
 }
 @-moz-keyframes bganim {
  from {background-position:0px;} to { background-position:40px;}
 }
 @-webkit-keyframes bganim {
  from {background-position:0px;} to { background-position:40px;}
 }
 @-o-keyframes bganim {
  from {background-position:0px;} to { background-position:40px;}
 }
 @-ms-keyframes bganim {
  from {background-position:0px;} to { background-position:40px;}
 }
Salin selepas log masuk

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

相关推荐:

php 无限分类 树形数据格式化代码实例详解

php实现的SSO单点登录系统接入功能实例分析

php与python实现的线程池多线程爬虫功能实例详解

Atas ialah kandungan terperinci php上传进度条详细解读(推荐). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan