php實現(xiàn)上傳進度條的方法:首先向服務器端上傳一個文件;然后用PHP將此次文件上傳的詳細信息存儲在session當中;接著用Ajax周期性的請求一個服務器端腳本;最后通過瀏覽器端的Javascript顯示更新進度條即可。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于做網(wǎng)站、成都網(wǎng)站設計、芒康網(wǎng)絡推廣、微信小程序、芒康網(wǎng)絡營銷、芒康企業(yè)策劃、芒康品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供芒康建站搭建服務,24小時服務熱線:13518219792,官方網(wǎng)址:vcdvsql.cn
推薦:《PHP視頻教程》
實現(xiàn)文件上傳進度條基本是依靠JS插件或HTML5的File API來完成,其實PHP配合ajax也能實現(xiàn)此功能。
PHP手冊對于session上傳進度是這么介紹的:
當 session.upload_progress.enabled INI 選項開啟時,PHP 能夠在每一個文件上傳時監(jiān)測上傳進度。 這個信息對上傳請求自身并沒有什么幫助,但在文件上傳時應用可以發(fā)送一個POST請求到終端(例如通過XHR)來檢查這個狀態(tài) 當一個上傳在處理中,同時POST一個與INI中設置的session.upload_progress.name同名變量時,上傳進度可以在$_SESSION中獲得。 當PHP檢測到這種POST請求時,它會在$_SESSION中添加一組數(shù)據(jù), 索引是 session.upload_progress.prefix 與 session.upload_progress.name連接在一起的值。 通常這些鍵值可以通過讀取INI設置來獲得,例如 <?php $key = ini_get("session.upload_progress.prefix") . ini_get("session.upload-progress.name"); var_dump($_SESSION[$key]); ?> 通過將$_SESSION[$key]["cancel_upload"]設置為TRUE,還可以取消一個正在處理中的文件上傳。 當在同一個請求中上傳多個文件,它僅會取消當前正在處理的文件上傳和未處理的文件上傳,但是不會移除那些已經(jīng)完成的上傳。 當一個上傳請求被這么取消時,$_FILES中的error將會被設置為 UPLOAD_ERR_EXTENSION。 session.upload_progress.freq 和 session.upload_progress.min_freq INI選項控制了上傳進度信息應該多久被重新計算一次。 通過合理設置這兩個選項的值,這個功能的開銷幾乎可以忽略不計。 注意:為了使這個正常工作,web服務器的請求緩沖區(qū)需要禁用,否則 PHP可能僅當文件完全上傳完成時才能收到文件上傳請求。 已知會緩沖這種大請求的程序有Nginx。
下面原理介紹:
當瀏覽器向服務器端上傳一個文件時,PHP將會把此次文件上傳的詳細信息(如上傳時間、上傳進度等)存儲在session當中。然后,隨著上傳的進行,周期性的更新session中的信息。這樣,瀏覽器端就可以使用Ajax周期性的請求一個服務器端腳本,由該腳本返回session中的進度信息;瀏覽器端的Javascript即可根據(jù)這些信息顯示/更新進度條了。
php.ini需配置以下選項
session.upload_progress.enabled = "1" session.upload_progress.cleanup = "1" session.upload_progress.prefix = "upload_progress_" session.upload_progress.name = "PHP_SESSION_UPLOAD_PROGRESS" session.upload_progress.freq = "1%" session.upload_progress.min_freq = "1"
其中enabled控制upload_progress功能的開啟與否,默認開啟;
cleanup 則設置當文件上傳的請求提交完成后,是否清除session的相關信息,默認開啟,如果需要調(diào)試$_SESSION,則應該設為Off。
prefix 和 name 兩項用來設置進度信息在session中存儲的變量名/鍵名。
freq 和 min_freq 兩項用來設置服務器端對進度信息的更新頻率。合理的設置這兩項可以減輕服務器的負擔。
在上傳文件的表單中,需要為該次上傳設置一個標識符,并在接下來的過程中使用該標識符來引用進度信息。
具體的,在上傳表單中需要有一個隱藏的input,它的name屬性為php.ini中 session.upload_progress.name 的值;它的值為一個由你自己定義的標識符。如下:
代碼如下:
<input type="hidden" name="<?php echo ini_get('session.upload_progress.name'); ?>" value="test" />
接到文件上傳的表單后,PHP會在$_SESSION變量中新建鍵,鍵名是一個將session.upload_progress.prefix的值與上面自定義的標識符連接后得到的字符串,可以這樣得到:
代碼如下:
$name = ini_get('session.upload_progress.name'); $key = ini_get('session.upload_progress.prefix') . $_POST[$name]; $_SESSION[$key]; // 這里就是此次文件上傳的進度信息了
$_SESSION[$key]這個變量的結(jié)構(gòu)是這樣的:
array ( 'upload_progress_test' => array ( 'start_time' => 1491494993, // 開始時間 'content_length' => 1410397, // POST請求的總數(shù)據(jù)長度 'bytes_processed' => 1410397, // 已收到的數(shù)據(jù)長度 'done' => true, // 請求是否完成 true表示完成,false未完成 'files' => array ( 0 => array ( 'field_name' => 'file1', 'name' => 'test.jpg', 'tmp_name' => 'D:\\\\wamp\\\\tmp\\\\phpE181.tmp', 'error' => 0, 'done' => true, 'start_time' => 1491494993, 'bytes_processed' => 1410096, ), ), ), );
這樣,我們就可以使用其中的 content_length 和 bytes_processed 兩項來得到進度百分比。
原理介紹完了,下面我們來完整的實現(xiàn)一個基于PHP和Javascript的文件上傳進度條。
上傳表單index.php
<?php session_start(); ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>PHP(5.4) Session 上傳進度 Demo</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="keywords" content=""/> <meta name="description" content=""/> <meta name="author" content=""> <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> <style type="text/css"> body{ font-size:1em; color:#333; font-family: "宋體", Arial, sans-serif; } h1, h2, h3, h4, h5, h6{ font-family: "宋體", Georgia, serif; color:#000; line-height:1.8em; margin:0; } h1{ font-size:1.8em; } #wrap{ margin-top:15px; margin-bottom:50px; background:#fff; border-radius:5px; box-shadow:inset 0 0 3px #000, 0 0 3px #eee; } #header{ border-radius:5px 5px 0 0; box-shadow:inset 0 0 3px #000; padding:0 15px; color:#fff; background: #333333; } #header h1{ color:#fff; } #article{ padding:0 15px; } #footer{ text-align:center; border-top:1px solid #ccc; border-radius:0 0 5px 5px; } .progress { width: 100%; border: 1px solid #4da8fe; border-radius: 40px; height: 20px; position: relative; } .progress .labels { position: relative; text-align: center; } .progress .bar { position: absolute; left: 0; top: 0; background: #4D90FE; height: 20px; line-height:20px; border-radius: 40px; min-width: 20px; } .report-file { display: block; position: relative; width: 120px; height: 28px; overflow: hidden; border: 1px solid #428bca; background: none repeat scroll 0 0 #428bca; color: #fff; cursor: pointer; text-align: center; float: left; margin-right:5px; } .report-file span { cursor: pointer; display: block; line-height: 28px; } .file-prew { cursor: pointer; position: absolute; top: 0; left:0; width: 120px; height: 30px; font-size: 100px; opacity: 0; filter: alpha(opacity=0); } .container{ padding-left:0; padding-right:0; margin:0 auto; } </style> </head> <body> <p id="wrap" class="container"> <p id="header"> <h1>Session上傳進度 Demo</h1> </p> <p id="article"> <form id="upload-form" action="upload.php" method="POST" enctype="multipart/form-data" style="margin:15px 0" target="hidden_iframe"> <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="test"/> <p class="report-file"> <span>上傳文件…</span><input tabindex="3" size="3" name="file1" class="file-prew" type="file" onchange="document.getElementById('textName').value=this.value"> </p> <input type="text" id="textName" style="height: 28px;border:1px solid #f1f1f1" /> <p> <input type="submit" class="btn btn-default" value="上傳"/> </p> </form> <p id="progress" class="progress" style="margin-bottom:15px;display:none;"> <p class="bar" style="width:0%;"></p> <p class="labels">0%</p> </p> </p> <!-- #article --> <p id="footer"> <p> </p> </p> </p><!-- #wrap --> <iframe id="hidden_iframe" name="hidden_iframe" src="about:blank" style="display:none;"></iframe> <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <script type="text/javascript"> function fetch_progress() { $.get('progress.php', {'<?php echo ini_get("session.upload_progress.name"); ?>': 'test'}, function (data) { var progress = parseInt(data); $('#progress .labels').html(progress + '%'); $('#progress .bar').css('width', progress + '%'); if (progress < 100) { setTimeout('fetch_progress()', 500); } else { $('#progress .labels').html('100%'); } }, 'html'); } $('#upload-form').submit(function () { $('#progress').show(); //圖片比較小,看不出進度條加載效果,初始設33% $('#progress .labels').html('33%'); $('#progress .bar').css('width', '33%'); setTimeout('fetch_progress()', 500); }); </script> </body> </html>
注意表單中的session.upload_progress.name隱藏項,值設置為了test。表單中僅有一個文件上傳input,如果需要,你可以添加多個。
這里需要特別注意一下表單的target屬性,這里設置指向了一個當前頁面中的iframe。這一點很關鍵,通過設置target屬性,讓表單提交后的頁面顯示在iframe中,從而避免當前的頁面跳轉(zhuǎn)。因為我們還得在當前頁面顯示進度條呢。
上傳文件upload.php
<?php /** * 上傳文件 */ if(is_uploaded_file($_FILES['file1']['tmp_name'])){ //unlink($_FILES['file1']['tmp_name']); $fileName = 'pic_' . date('YmdHis') . mt_rand(10000,99999); $ext = substr($_FILES['file1']['name'], strrpos($_FILES['file1']['name'], '.')); move_uploaded_file($_FILES['file1']['tmp_name'], $fileName . $ext); }
ajax獲取上傳進度progress.php
<?php /** * AJAX獲取上傳文件進度 */ session_start(); $i = ini_get('session.upload_progress.name'); //session.upload_progress.name = "PHP_SESSION_UPLOAD_PROGRESS" $key = ini_get("session.upload_progress.prefix") . $_GET[$i]; //session.upload_progress.prefix = "upload_progress_" . 'test' if (!empty($_SESSION[$key])) { $current = $_SESSION[$key]["bytes_processed"]; // 已收到的數(shù)據(jù)長度 $total = $_SESSION[$key]["content_length"]; // POST請求的總數(shù)據(jù)長度 echo $current < $total ? ceil($current / $total * 100) : 100; }else{ echo 100; }
注意事項:
1.input標簽的位置name為session.upload_progress.name的input標簽一定要放在文件input <input type="file" /> 的前面。
2.通過設置 $_SESSION[$key]['cancel_upload'] = true 可取消當次上傳。但僅能取消正在上傳的文件和尚未開始的文件。已經(jīng)上傳成功的文件不會被刪除。
3.應該通過 setTimeout() 來調(diào)用 fetch_progress(),這樣可以確保一次請求返回之后才開始下一次請求。如果使用 setInterval() 則不能保證這一點,有可能導致進度條出現(xiàn)'不進反退'。
網(wǎng)頁題目:php如何實現(xiàn)上傳進度條
網(wǎng)站網(wǎng)址:http://vcdvsql.cn/article6/chsiig.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站維護、網(wǎng)站建設、Google、響應式網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)