php實(shí)現(xiàn)簡(jiǎn)單的上傳進(jìn)度條
Web上傳文件的三種解決方案分享給大家:
這里我要使用的是form法。通過為表單元素設(shè)置enctype=”multipart/form-data”屬性,讓表單提交的數(shù)據(jù)以二進(jìn)制編碼的方式提交,在接收此請(qǐng)求的Servlet中用二進(jìn)制流來獲取內(nèi)容,就可以取得上傳文件的內(nèi)容,從而實(shí)現(xiàn)文件的上傳。
表單元素的enctype屬性指定的是表單數(shù)據(jù)的編碼方式,該屬性有3個(gè)值:
在網(wǎng)上找到了兩種方式,PHP配合apc實(shí)現(xiàn)和利用uploadprogress實(shí)現(xiàn),這次我要使用的是uploadprogress,點(diǎn)擊地址可以下載到php相應(yīng)版本的dll。安裝php_uploadprogress.dll擴(kuò)展,重啟apache。
進(jìn)度條實(shí)現(xiàn)原理:
這里用到了一個(gè)iframe無刷新上傳文件的方法。
上傳完成后的樣子如圖:
<body> <div style="padding:20px"> <form action="action.php" enctype="multipart/form-data" method="post" target="iframeUpload"> <iframe name="iframeUpload" width="400" height="400" frameborder='1'></iframe> <input type="hidden" name="UPLOAD_IDENTIFIER" value="1" /> <input id="file1" name="file1" type="file"/> <input value="上傳" type="submit" onclick="startProgress()"/> </form> </div> <div style="width: 500px; height: 20px;border:1px solid red"> <div style="position: relative; height: 20px; background-color: purple; width: 0%;" class="barinner"></div> </div> <div id='showNum'></div> <div class="prbar"> <div class="prpos barinner"></div> </div> </body>
上面的HTML代碼中要注意下UPLOAD_IDENTIFIER,這個(gè)是用來定位查看哪個(gè)文件的上傳進(jìn)度的。我這里就寫死為一個(gè)1,大家可以寫成一個(gè)php生成的隨機(jī)數(shù)。下面是JS腳本:
var proNum=0; var loop=0; var progressResult = ""; var interval; function sendURL() { $.ajax({ type : 'GET', url : "getprogress.php", async : true, cache : false, dataType : 'json', data: "progress_key=" + $('input[name=UPLOAD_IDENTIFIER]').val(), success : function(e) { proNum=parseInt(e); if(e){ $('.barinner').css('width', proNum+"%"); $('#showNum').html(proNum+"%"); setTimeout("getProgress()", 200); }else{ if(interval == 1){ $('.barinner').css('width', "100%"); $('#showNum').html("100%"); } } } }); } function getProgress(){ loop++; sendURL(); } function startProgress(){ interval = 1; $('.barinner').css('width', proNum+"%"); $('#showNum').html(proNum+"%"); setTimeout("getProgress()", 500); }
下面是getprogress.php文件中的內(nèi)容:
<?php if (function_exists("uploadprogress_get_info")) { $info = uploadprogress_get_info($_GET['progress_key']); if(!empty($info)){ if(($info['bytes_uploaded'] < $info['bytes_total']) && !empty($info['bytes_uploaded']) && !empty($info['bytes_total'])){ $proNum = floor(($info['bytes_uploaded']/$info['bytes_total'])*100); }else{ $proNum = 100; } echo $proNum; }else{ echo 0; } }
在上傳完成后,我展示了兩種進(jìn)度條的CSS,第二種是用最新的CSS3寫的。用到了一些CSS3的動(dòng)畫屬性。
.prbar { margin:5px; width:500px; background-color:#dddddd; overflow:hidden; /* 邊框效果 */ border: 1px solid #bbbbbb; -moz-border-radius: 15px; border-radius: 15px; /* 為進(jìn)度條增加陰影效果 */ -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 進(jìn)度條漸變 */ 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;} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- php 下 html5 XHR2 + FormData + File API 上傳文件操作實(shí)例分析
- PHP使用HTML5 FileApi實(shí)現(xiàn)Ajax上傳文件功能示例
- php+html5實(shí)現(xiàn)無刷新圖片上傳教程
- 使用PHP和HTML5 FormData實(shí)現(xiàn)無刷新文件上傳教程
- php+html5+ajax實(shí)現(xiàn)上傳圖片的方法
- php+html5使用FormData對(duì)象提交表單及上傳圖片的方法
- php 使用html5實(shí)現(xiàn)多文件上傳實(shí)例
- PHP 文件上傳進(jìn)度條的兩種實(shí)現(xiàn)方法的代碼
- php上傳文件并顯示上傳進(jìn)度的方法
- PHP+Ajax無刷新帶進(jìn)度條圖片上傳示例
- PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
- php 使用html5 XHR2實(shí)現(xiàn)上傳文件與進(jìn)度顯示功能示例
相關(guān)文章
PHP中信息格式化操作詳解(MessageFormatter類)
這篇文章主要給大家介紹了關(guān)于PHP中信息格式化操作的相關(guān)資料,主要運(yùn)用的是專門用于信息格式化的MessageFormatter類,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07Linux環(huán)境下搭建php開發(fā)環(huán)境的操作步驟
本篇文章是對(duì)Linux環(huán)境下搭建php開發(fā)環(huán)境的操作步驟進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php中目錄操作opendir()、readdir()及scandir()用法示例
這篇文章主要介紹了php中目錄操作opendir()、readdir()及scandir()用法,結(jié)合具體實(shí)例形式分析了PHP使用opendir()、readdir()及scandir()讀取目錄的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06php中實(shí)現(xiàn)獲取隨機(jī)數(shù)組列表的自定義函數(shù)
這篇文章主要介紹了php中實(shí)現(xiàn)獲取隨機(jī)數(shù)組列表的自定義函數(shù),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04phpinfo() 中 Local Value(局部變量)Master Value(主變量) 的區(qū)別
這篇文章主要介紹了phpinfo() 中 Local Value(局部變量)Master Value(主變量) 的區(qū)別,需要的朋友可以參考下2016-02-02完美解決thinkphp唯一索引重復(fù)時(shí)出錯(cuò)的問題
下面小編就為大家?guī)硪黄昝澜鉀Qthinkphp唯一索引重復(fù)時(shí)出錯(cuò)的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03微信公眾號(hào)開發(fā)之微信公共平臺(tái)消息回復(fù)類實(shí)例
這篇文章主要介紹了微信公眾號(hào)開發(fā)之微信公共平臺(tái)消息回復(fù)類,給出了其完整實(shí)例,并附有注釋說明便于理解與運(yùn)用,對(duì)于微信公眾號(hào)的開發(fā)來說非常具有參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11