最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP使用Session實(shí)現(xiàn)上傳進(jìn)度功能詳解

 更新時(shí)間:2019年08月06日 10:01:25   作者:cqingt  
這篇文章主要介紹了PHP使用Session實(shí)現(xiàn)上傳進(jìn)度功能,結(jié)合實(shí)例形式詳細(xì)分析了session上傳進(jìn)度顯示相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了PHP使用Session實(shí)現(xiàn)上傳進(jìn)度功能。分享給大家供大家參考,具體如下:

實(shí)現(xiàn)文件上傳進(jìn)度條基本是依靠JS插件或HTML5的File API來完成,其實(shí)PHP配合ajax也能實(shí)現(xiàn)此功能。

PHP手冊對于session上傳進(jìn)度是這么介紹的:

當(dāng) session.upload_progress.enabled INI 選項(xiàng)開啟時(shí),PHP 能夠在每一個文件上傳時(shí)監(jiān)測上傳進(jìn)度。 這個信息對上傳請求自身并沒有什么幫助,但在文件上傳時(shí)應(yīng)用可以發(fā)送一個POST請求到終端(例如通過XHR)來檢查這個狀態(tài)

當(dāng)一個上傳在處理中,同時(shí)POST一個與INI中設(shè)置的session.upload_progress.name同名變量時(shí),上傳進(jìn)度可以在$_SESSION中獲得。 當(dāng)PHP檢測到這種POST請求時(shí),它會在$_SESSION中添加一組數(shù)據(jù), 索引是 session.upload_progress.prefixsession.upload_progress.name連接在一起的值。 通常這些鍵值可以通過讀取INI設(shè)置來獲得,例如

<?php
$key = ini_get("session.upload_progress.prefix") . ini_get("session.upload-progress.name");
var_dump($_SESSION[$key]);
?>

通過將$_SESSION[$key]["cancel_upload"]設(shè)置為TRUE,還可以取消一個正在處理中的文件上傳。 當(dāng)在同一個請求中上傳多個文件,它僅會取消當(dāng)前正在處理的文件上傳和未處理的文件上傳,但是不會移除那些已經(jīng)完成的上傳。 當(dāng)一個上傳請求被這么取消時(shí),$_FILES中的error將會被設(shè)置為 UPLOAD_ERR_EXTENSION。

session.upload_progress.freqsession.upload_progress.min_freq INI選項(xiàng)控制了上傳進(jìn)度信息應(yīng)該多久被重新計(jì)算一次。 通過合理設(shè)置這兩個選項(xiàng)的值,這個功能的開銷幾乎可以忽略不計(jì)。

注意:為了使這個正常工作,web服務(wù)器的請求緩沖區(qū)需要禁用,否則 PHP可能僅當(dāng)文件完全上傳完成時(shí)才能收到文件上傳請求。 已知會緩沖這種大請求的程序有Nginx。

下面原理介紹:

  當(dāng)瀏覽器向服務(wù)器端上傳一個文件時(shí),PHP將會把此次文件上傳的詳細(xì)信息(如上傳時(shí)間、上傳進(jìn)度等)存儲在session當(dāng)中。然后,隨著上傳的進(jìn)行,周期性的更新session中的信息。這樣,瀏覽器端就可以使用Ajax周期性的請求一個服務(wù)器端腳本,由該腳本返回session中的進(jìn)度信息;瀏覽器端的Javascript即可根據(jù)這些信息顯示/更新進(jìn)度條了。

php.ini需配置以下選項(xiàng)

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功能的開啟與否,默認(rèn)開啟;
  cleanup 則設(shè)置當(dāng)文件上傳的請求提交完成后,是否清除session的相關(guān)信息,默認(rèn)開啟,如果需要調(diào)試$_SESSION,則應(yīng)該設(shè)為Off。
  prefix 和 name 兩項(xiàng)用來設(shè)置進(jìn)度信息在session中存儲的變量名/鍵名。
  freq 和 min_freq 兩項(xiàng)用來設(shè)置服務(wù)器端對進(jìn)度信息的更新頻率。合理的設(shè)置這兩項(xiàng)可以減輕服務(wù)器的負(fù)擔(dān)。
  在上傳文件的表單中,需要為該次上傳設(shè)置一個標(biāo)識符,并在接下來的過程中使用該標(biāo)識符來引用進(jìn)度信息。

  具體的,在上傳表單中需要有一個隱藏的input,它的name屬性為php.ini中 session.upload_progress.name 的值;它的值為一個由你自己定義的標(biāo)識符。如下:
 代碼如下:

<input type="hidden" name="<?php echo ini_get('session.upload_progress.name'); ?>" value="test" />

接到文件上傳的表單后,PHP會在$_SESSION變量中新建鍵,鍵名是一個將session.upload_progress.prefix的值與上面自定義的標(biāo)識符連接后得到的字符串,可以這樣得到:

代碼如下:

$name = ini_get('session.upload_progress.name');
$key = ini_get('session.upload_progress.prefix') . $_POST[$name];
$_SESSION[$key]; // 這里就是此次文件上傳的進(jìn)度信息了
$_SESSION[$key]這個變量的結(jié)構(gòu)是這樣的:

array (
  'upload_progress_test' => array (
    'start_time' => 1491494993,  // 開始時(shí)間
    '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àng)來得到進(jìn)度百分比。

原理介紹完了,下面我們來完整的實(shí)現(xiàn)一個基于PHP和Javascript的文件上傳進(jìn)度條。

上傳表單index.php

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>PHP(5.4) Session 上傳進(jìn)度 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  rel="external nofollow" 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>
<div id="wrap" class="container">
  <div id="header">
    <h1>Session上傳進(jìn)度 Demo</h1>
  </div>
  <div 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"/>
      <div class="report-file">
        <span>上傳文件…</span><input tabindex="3" size="3" name="file1" class="file-prew" type="file" onchange="document.getElementById('textName').value=this.value">
      </div>
      <input type="text" id="textName" style="height: 28px;border:1px solid #f1f1f1" />
      <p>
        <input type="submit" class="btn btn-default" value="上傳"/>
      </p>
    </form>
    <div id="progress" class="progress" style="margin-bottom:15px;display:none;">
      <div class="bar" style="width:0%;"></div>
      <div class="labels">0%</div>
    </div>
  </div> <!-- #article -->
  <div id="footer">
    <p> </p>
  </div>
</div><!-- #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();
    //圖片比較小,看不出進(jìn)度條加載效果,初始設(shè)33%
    $('#progress .labels').html('33%');
    $('#progress .bar').css('width', '33%');
    setTimeout('fetch_progress()', 500);
  });
</script>
</body>
</html>

  注意表單中的session.upload_progress.name隱藏項(xiàng),值設(shè)置為了test。表單中僅有一個文件上傳input,如果需要,你可以添加多個。
  這里需要特別注意一下表單的target屬性,這里設(shè)置指向了一個當(dāng)前頁面中的iframe。這一點(diǎn)很關(guān)鍵,通過設(shè)置target屬性,讓表單提交后的頁面顯示在iframe中,從而避免當(dāng)前的頁面跳轉(zhuǎn)。因?yàn)槲覀冞€得在當(dāng)前頁面顯示進(jì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獲取上傳進(jìn)度progress.php

<?php
/**
 * AJAX獲取上傳文件進(jìn)度
 */
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;
}

注意事項(xiàng):

1.input標(biāo)簽的位置name為session.upload_progress.name的input標(biāo)簽一定要放在文件input <input type="file" /> 的前面。

2.通過設(shè)置 $_SESSION[$key]['cancel_upload'] = true 可取消當(dāng)次上傳。但僅能取消正在上傳的文件和尚未開始的文件。已經(jīng)上傳成功的文件不會被刪除。

3.應(yīng)該通過 setTimeout() 來調(diào)用 fetch_progress(),這樣可以確保一次請求返回之后才開始下一次請求。如果使用 setInterval() 則不能保證這一點(diǎn),有可能導(dǎo)致進(jìn)度條出現(xiàn)'不進(jìn)反退'。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP目錄操作技巧匯總》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php中in_array函數(shù)用法探究

    php中in_array函數(shù)用法探究

    這篇文章主要介紹了php中in_array函數(shù)用法,對in_array函數(shù)參數(shù)匹配的用法進(jìn)行了較為深入的探究,有助于較為全面的理解in_array函數(shù)的用法,需要的朋友可以參考下
    2014-11-11
  • PHP備份/還原MySQL數(shù)據(jù)庫的代碼

    PHP備份/還原MySQL數(shù)據(jù)庫的代碼

    之前是采用PHP讀取數(shù)據(jù)庫結(jié)構(gòu)和內(nèi)容,然后寫文件,這樣可能會導(dǎo)致導(dǎo)出的文件不一定能百分百導(dǎo)入到MySQL中去,想想之后采用MySQL自帶的導(dǎo)入導(dǎo)出命令最保險(xiǎn)
    2011-01-01
  • PHP實(shí)現(xiàn)加密的幾種方式介紹

    PHP實(shí)現(xiàn)加密的幾種方式介紹

    這篇文章主要介紹了PHP實(shí)現(xiàn)加密的幾種方式,非常全面實(shí)用,都是項(xiàng)目中經(jīng)常需要用到的,需要的朋友可以參考下
    2015-02-02
  • redirect_uri參數(shù)錯誤的解決方法(必看)

    redirect_uri參數(shù)錯誤的解決方法(必看)

    下面小編就為大家?guī)硪黄猺edirect_uri參數(shù)錯誤的解決方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • 利用curl 多線程 模擬 并發(fā)的詳解

    利用curl 多線程 模擬 并發(fā)的詳解

    本篇文章是對利用curl多線程模擬并發(fā)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP pthreads v3下worker和pool的使用方法示例

    PHP pthreads v3下worker和pool的使用方法示例

    這篇文章主要介紹了PHP pthreads v3下worker和pool的使用方法,結(jié)合實(shí)例形式分析了PHP pthreads v3下worker和pool的基本功能、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • php變量范圍介紹

    php變量范圍介紹

    變量的范圍即它定義的上下文背景(也就是它生效的范圍)。大部分的 PHP 變量只有一個單獨(dú)的范圍。這個單獨(dú)的范圍跨度同樣包含了 include 和 require 引入的文件
    2012-10-10
  • 簡單的移動設(shè)備檢測PHP腳本代碼

    簡單的移動設(shè)備檢測PHP腳本代碼

    簡單的移動設(shè)備檢測PHP腳本代碼,需要的朋友可以參考下。
    2011-02-02
  • PHP $_FILES中error返回值詳解

    PHP $_FILES中error返回值詳解

    用PHP上傳文件時(shí),我們會用程序去監(jiān)聽瀏覽器發(fā)送過來的文件信息,首先會通 過$_FILES[fieldName]['error']的不同數(shù)值來判斷此欲上傳的文件狀態(tài)是否正常。$_FILES[fieldName] ['error']==0代表一切正常,其它數(shù)值的具體含義請參考下面一段程序的注釋部分,如有翻譯不到之處還望指正
    2014-01-01
  • PHP number_format() 函數(shù)定義和用法

    PHP number_format() 函數(shù)定義和用法

    number_format() 函數(shù)通過千位分組來格式化數(shù)字
    2012-06-06

最新評論

湖南省| 靖远县| 自贡市| 彭山县| 通榆县| 乌什县| 霍山县| 石城县| 隆化县| 民县| 汉中市| 鄂伦春自治旗| 宿州市| 新乡市| 松潘县| 保康县| 宜阳县| 上饶市| 崇文区| 同心县| 伊通| 郁南县| 肇庆市| 新乡县| 广河县| 台北市| 铜陵市| 宁城县| 禹城市| 巨鹿县| 启东市| 青神县| 若羌县| 武宁县| 繁峙县| 闵行区| 太白县| 五常市| 屏山县| 靖西县| 梓潼县|