大文件分片上傳,斷點續傳,秒傳 實現


前段時間做視頻上傳業務,通過網頁上傳視頻到服務器。
視頻大小 小則幾十M,大則 1G+,以一般的HTTP請求發送數據的方式的話,會遇到的問題:1,文件過大,超出服務端的請求大小限制;2,請求時間過長,請求超時;3,傳輸中斷,必須重新上傳導致前功盡棄;
 
解決方案:
1,修改服務端上傳的限制配置;Nginx 以及 PHP 的上傳文件限制 不宜過大,一般5M 左右為好;
2,大文件分片,一片一片的傳到服務端,再由服務端合並。這么做的好處在於一旦上傳失敗只是損失一個分片而已,不用整個文件重傳,而且每個分片的大小可以控制在4MB以內,服務端限制在4M即可。

前端

Web前端可使用百度的 webuploader H5大文件分片上傳插件;官網地址:http://fex.baidu.com/webuploader/ 

<div class="section section6 section5">
    <div class="part1"><a href="javascript:;" target="_blank" class="part1__btn">批量刪除</a><span class="part1__txt"><em class="part1__num" id="upload_num">0</em>個視頻,共 <em class="part1__num" id="upload_size">0M</em></span></div>
    <table class="section5__table">
        <tbody id="thelist">
            <tr class="thead">
                <th class="col1 allCkeck"><input type="checkbox" name="" class="col1__checkBox"/>視頻名稱</th><th class="col2">視頻大小</th><th class="col3">視頻分類</th><th class="col4">狀態</th><th class="col5">進度</th><th>操作</th>
            </tr>
        </tbody>
    </table>
    <div class="selFile" id="selFile">
        <div id="drag_tips">
            <div id="btns__add2"></div>
            <h2 class="txt1">選擇視頻文件</h2>
            <span class="txt2">或直接將文件拖拽至此窗口</span>
        </div>
    </div>
    <div class="btns"><span class="btns__add" id="btns__add">+添加視頻文件</span><span class="btns__upload btns__upload-start" id="uploadBtn"><i class="btns__upload_icon"></i>開始上傳視頻</span></div>
</div>

 

//引入插件
<
script type="text/javascript" src="media/js/lib/webuploader/js/webuploader.min.js"></script>

upload.js

  1 // 文件上傳
  2 jQuery(function() {
  3     var $ = jQuery,
  4         $list = $('#thelist'),
  5         $btn = $('#upload-start'),
  6         $thead = $('.thead'),
  7         $part_btn = $('.part1__btn'), //批量上傳按鈕
  8         state = 'pending',
  9         fileCount = 0, //上傳文件總數
 10         fileSize = 0,//上傳文件的總大小
 11     // 上傳按鈕
 12         $upload = $('#uploadBtn'),
 13     // 所有文件的進度信息,key為file id
 14         percentages = {},
 15     // 所有文件的md5,key為file id
 16         md5Obj = {},
 17     // 可能有pedding, ready, uploading, confirm, done.
 18         state = 'pedding',
 19         uploader;
 20 
 21     //瀏覽器關閉提醒
 22     window.is_confirm = false;
 23     $(window).bind('beforeunload', function(){
 24         // 只有在標識變量is_confirm不為false時,才彈出確認提示
 25         if(window.is_confirm !== false)
 26             return '正在上傳視頻,該操作將丟失視頻,是否繼續?';
 27     })
 28 
 29     if ( !WebUploader.Uploader.support() ) {
 30         alert( 'Web Uploader 不支持您的瀏覽器!如果你使用的是IE瀏覽器,請嘗試升級瀏覽器');
 31         throw new Error( 'WebUploader does not support the browser you are using.' );
 32     }
 33 
 34     $(".pop2 .btns__sure").click(function(){
 35         $('.popup,.pop').hide();
 36     });
 37 
 38     uploader = WebUploader.create({
 39         //拖拽容器
 40         dnd:'#selFile',
 41 
 42         // 不壓縮image
 43         resize: false,
 44 
 45         // swf文件路徑
 46         swf: '/media/js/lib/webuploader/js/Uploader.swf',
 47 
 48         // 文件接收服務端。
 49         server: '/service/upload/upload_file',
 50         //server:'http://vod.test.4399sy.com/service/upload/ssl_upload_file',
 51         formData: {
 52             file_id: 'file',
 53             guid:new Date().getTime() + Math.ceil(Math.random()*100),
 54             file_name:''
 55         },
 56 
 57         // 選擇文件的按鈕。可選。
 58         // 內部根據當前運行是創建,可能是input元素,也可能是flash.
 59         pick: {
 60             id:'#btns__add',
 61             innerHTML:"+添加視頻文件"
 62         },
 63 
 64         // 開起分片上傳。
 65         chunked: true,
 66 
 67         //如果要分片,分多大一片2M
 68         chunkSize:2*1024*1024,
 69 
 70         //上傳文件的類型
 71         accept:{
 72             title: 'Videos',
 73             extensions: 'mp4,avi,flv',
 74             mimeTypes: 'video/*'
 75         },
 76         //驗證文件總數量, 超出則不允許加入隊列。
 77         fileNumLimit: 10,
 78         //單個文件上傳的大小限制 2G
 79         fileSingleSizeLimit:2*1024*1024*1024,
 80 
 81     });
 82 
 83     //添加文件具體函數
 84     function addFile( file ){
 85         var data = new Date(),
 86             month = (data.getMonth()+1)<10 ? '0'+(data.getMonth()+1) : (data.getMonth()+1),
 87             day = data.getDate()<10 ? '0'+ data.getDate(): data.getDate(),
 88             time = data.getFullYear() + "-" + month + "-" + day,
 89             $tr = $('<tr class="toBeUploaded" id="'+file.id+'"></tr>'),
 90             $td = $('<td class="col1"><input type="checkbox" name="" class="col1__checkBox"/><input type="text" value="'+ file.name +'" name="" class="name"/></td><td class="col2">'+convert_size(file.size)+'</td><td class="col3"><select class="class_id">'+ class_options +'</select></td><td class="col4">讀取視頻中</td><td class="col5">0%</td><td class="col6"><ul><li class="view"><a target="_blank" href="javascript:;">查看</a></li><li class="delete">刪除</li></ul></td>').appendTo($tr),
 91             $state = $tr.find('td.col4'),
 92             $prgress = $tr.find('td.col5'),
 93             $delbtn = $tr.find('li.delete');
 94 
 95         $("#selFile").hide();
 96 
 97         if ( file.getStatus() === 'invalid' ) {
 98             switch( file.statusText ) {
 99                 case 'exceed_size':
100                     text = '文件大小超出';
101                     break;
102 
103                 case 'interrupt':
104                     text = '上傳暫停';
105                     break;
106 
107                 default:
108                     text = '上傳失敗,請重試';
109                     break;
110             }
111             showError(text);
112         } else {
113             // @todo lazyload
114             percentages[ file.id ] = [ file.size, 0 ];
115             file.rotation = 0;
116         }
117 
118         file.on('statuschange', function( cur, prev ) {
119             if ( prev === 'progress' ) {
120                 //上傳成功
121             } else if ( prev === 'queued' ) {
122                 // 開始上傳
123             }
124 
125             // 成功
126             if ( cur === 'error' || cur === 'invalid' ) {
127                 console.log( file.statusText );
128                 showError( file.statusText );
129                 percentages[ file.id ][ 1 ] = 1;
130             } else if ( cur === 'interrupt' ) {
131                 showError( 'interrupt' );
132             } else if ( cur === 'queued' ) {
133                 percentages[ file.id ][ 1 ] = 0;
134             } else if ( cur === 'progress' ) {
135             //   正在上傳
136 
137             } else if ( cur === 'complete' ) {
138             //   上傳完成
139 
140             }
141 
142             $tr.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
143         });
144         $delbtn.on('click',function(){
145             uploader.removeFile( file );
146         });
147         $tr.appendTo($list);
148         //$tr.insertAfter($thead);
149     }
150 
151     // 負責view的銷毀
152     function removeFile( file ) {
153         var $tr = $('#'+file.id);
154 
155         delete percentages[ file.id ];
156         $tr.off().find('.col6').off().end().remove();
157     }
158 
159     function setState( val ) {
160         var file, stats;
161 
162         if ( val === state ) {
163             return;
164         }
165 
166         $upload.removeClass( 'state-' + state );
167         $upload.addClass( 'state-' + val );
168         state = val;
169 
170         switch ( state ) {
171             case 'pedding':
172                 uploader.refresh();
173                 break;
174 
175             case 'ready':
176                 uploader.refresh();
177                 break;
178 
179             case 'uploading':
180                 $upload.text( '暫停上傳' );
181                 break;
182             case 'paused':
183                 $upload.text( '繼續上傳' );
184                 break;
185 
186             case 'confirm':
187                 //$progress.hide();
188                 $upload.text( '開始上傳' ).addClass( 'disabled' );
189 
190                 stats = uploader.getStats();
191                 if ( stats.successNum && !stats.uploadFailNum ) {
192                     setState( 'finish' );
193                     return;
194                 }
195                 break;
196             case 'finish':
197                 stats = uploader.getStats();
198                 if ( stats.successNum ) {
199                     alert( '上傳成功' );
200                 } else {
201                     // 沒有成功的圖片,重設
202                     state = 'done';
203                     location.reload();
204                 }
205                 break;
206         }
207     }
208 
209 
210     // 當有文件添加進來的時候
211     uploader.on( 'fileQueued', function( file ) {
212         fileCount++;
213         fileSize += file.size;
214         $("#upload_num").text(fileCount);
215         $("#upload_size").text(convert_size(fileSize));
216         md5Obj[ file.id ] = '';
217         //獲取文件MD5 值
218         uploader.md5File( file )
219             // 及時顯示進度
220             .progress(function(percentage) {
221                 $( '#'+file.id ).find('.col4').text('讀取文件'+parseInt(percentage*100)+"%");
222             })
223             // 完成
224             .then(function(val) {
225             console.log('md5 result:', val);
226             md5Obj[ file.id ] = val;
227             $( '#'+file.id ).find('.col4').text('待上傳');
228             setState( 'ready' );
229         });
230         addFile( file );
231     });
232 
233     // 刪除文件
234     uploader.onFileDequeued = function( file ) {
235         fileCount--;
236         fileSize -= file.size;
237         $("#upload_num").text(fileCount);
238         $("#upload_size").text(convert_size(fileSize));
239         if ( !fileCount ) {
240             setState( 'pedding' );
241         }
242         removeFile( file );
243 
244     };
245 
246     // 添加“添加文件”的按鈕,
247     uploader.addButton({
248         id: '#btns__add2',
249         label: ''
250     });
251 
252     // 文件上傳過程中創建進度實時顯示。
253     uploader.onUploadProgress = function( file, percentage ) {
254         var $tr = $('#'+file.id),
255             $percent = $tr.find('td.col5'),
256             $state = $tr.find('td.col4');
257         percentage = parseInt(percentage*100);
258         if(! (percentage == 0 && percentage == 100)){
259             $state.text("正在上傳");
260         }
261         $percent.text( percentage + "%")
262         percentages[ file.id ][ 1 ] = percentage;
263     };
264 
265     //上傳前,請求服務端 判斷文件是否已經上傳過
266     uploader.on( 'uploadStart', function( file ) {
267         var type = 'POST';
268         var url = '/service/upload/determine_video_exist';
269         var request_data = {
270             'md5': md5Obj[ file.id ],
271             'type':1
272         };
273         var success = function(r) {
274             uploader.upload( file );
275             console.log(r);
276             if(r.code == 1) {
277                 uploader.skipFile( file );
278                 $( '#'+file.id ).find('.col4').text('視頻已存在');
279                 $( '#'+file.id ).find('.col5').text('100%');
280                 $('#'+file.id).find('.view').find('a').attr('href',playmain +'/?video_id='+ r.data.video_id);
281                 $('.pop2 .video_game').text("所在游戲:"+r.data.game_name);
282                 $('.pop2 .create_time').text("上傳時間:"+r.data.create_time);
283                 $('.pop').hide();
284                 $('.pop2').show();
285                 $('.popup').show();
286             }else if(r.code <= 0) {
287                 showError(r.msg);
288             }else {
289 
290             }
291         };
292         request(type, url, request_data, success);
293     });
294 
295     //當某個文件的分塊在發送前觸發,主要用來詢問是否要添加附帶參數,大文件在開起分片上傳的前提下此事件可能會觸發多次。
296     uploader.on('uploadBeforeSend', function (obj, data, headers) {
297         $tr = $("#"+data.id);
298         var file_name = $tr.find(".name").val();
299         var class_id = $tr.find("select.class_id").val();
300         var reg = /[1-9][0-9]*/g;
301         data.md5 = md5Obj[ obj.file.id ];
302         data.file_name = file_name;
303         data.class_id = class_id;
304         data.guid = data.guid + data.id.replace(/[^0-9]+/g, '');
305     });
306 
307     uploader.on( 'uploadSuccess', function( file ,res) {
308         if(res.code == 1){
309             $( '#'+file.id ).find('.col4').text('成功上傳');
310             console.log(res);
311             $('#'+file.id).find('.view').find('a').attr('href',playmain +'/?video_id='+ res.data.video_id);
312         }else if(res.code == 2) {
313             $( '#'+file.id ).find('.col4').text('視頻已存在');
314             console.log(res);
315             $('#'+file.id).find('.view').find('a').attr('href',playmain +'/?video_id='+ res.data.video_id);
316         }else {
317             showError(res.msg);
318         }
319     });
320 
321     uploader.on( 'uploadError', function( file,reason ) {
322         $( '#'+file.id ).find('.col4').text('上傳失敗');
323         console.log(reason);
324     });
325 
326     uploader.on( 'uploadComplete', function( file ) {
327         $( '#'+file.id ).find('.progress').fadeOut();
328     });
329 
330     uploader.on( 'all', function( type ) {
331         if ( type === 'startUpload' ) {
332             state = 'uploading';
333         } else if ( type === 'stopUpload' ) {
334             state = 'paused';
335         } else if ( type === 'uploadFinished' ) {
336             state = 'done';
337         }
338 
339         if ( state === 'uploading' ) {
340             window.is_confirm = true;
341             $('.toBeUploaded').addClass("uploaded").removeClass("toBeUploaded");
342             $('input.name').attr("disabled","disabled");
343             $('input.col1__checkBox').hide();
344             $('input').attr("disabled","disabled");
345             $('select.class_id').attr("disabled","disabled");
346             $('.btns__add').remove();
347             $upload.addClass("btns__upload-ing").removeClass("btns__upload-start").html('<i class="btns__upload_icon"></i>正在上傳視頻');
348 
349         } else if(state === 'done') {
350             window.is_confirm = false;
351             console.log("上傳完成");
352             $upload.addClass("btns__upload-start btns__upload-refresh").removeClass("btns__upload-ing").html('<i class="btns__upload_icon"></i>開始上傳視頻');
353         }
354     });
355 
356     /**
357      * 驗證文件格式以及文件大小
358      */
359     uploader.on("error",function (type){
360         var msg = ''
361         switch (type){
362             case "Q_TYPE_DENIED": msg = "請上傳mp4格式文件";break;
363             case "F_EXCEED_SIZE": msg = "文件大小不能超過1G";break;
364             case "Q_EXCEED_NUM_LIMIT" : msg = "一次最多能上傳10個文件";break;
365             default: msg='';
366         }
367         if(msg != ''){
368             showError(msg);
369         }
370     });
371 
372     $part_btn.on('click',function(){
373         $('td .col1__checkBox').each(function(){
374             if($(this).is(':checked')){
375                 var $tr = $(this).parents('tr');
376                 var id = $tr.attr('id');
377                 uploader.removeFile( id );
378             }
379         });
380     });
381     $upload.on('click', function() {
382         var isbreak = false;
383         $(".name").each(function(){
384             if(!$(this).val()|| $(this).val() == ''){
385                 isbreak = true;
386             }
387         })
388         if(isbreak){
389             showError("文件名不能存在為空");
390             return;
391         }
392         $("select.class_id").each(function(){
393             if(!$(this).val()|| $(this).val() == ''){
394                 isbreak = true;
395             }
396         })
397         if(isbreak){
398             showError("分類不能為空,請先添加分類");
399             return;
400         }
401         if ( $(this).hasClass( 'btns__upload-refresh' ) ) {
402             location.reload();
403         }
404         if ( $(this).hasClass( 'btns__upload-ing' ) ) {
405             return false;
406         }
407         var md5Ready = true;
408         $.each(md5Obj,function(index,item){
409             if(!item || item==''){
410                 md5Ready = false;
411             }
412         });
413         if(!md5Ready){
414             showError('文件尚未讀取完成,請耐心等待');
415             return false;
416         }
417         if ( state === 'ready' && md5Ready ) {
418             uploader.upload();
419         } else if ( state === 'paused' ) {
420             uploader.upload();
421         } else if ( state === 'uploading' ) {
422             uploader.stop();
423         }
424     });
425     $upload.addClass( 'state-' + state );
426 
427 });

 

后台(PHP)【僅分片上傳相關代碼】

  1     public function action_upload_file(){
  2         $file_id = R::string('file_id', 'file');
  3         $keepFileName = R::string('keepFileName', 0);
  4         $unsize_change = R::numeric('unsize_change',0);
  5         $id = R::string('id');   //插件每上傳一個視頻自帶id
  6         $guid = R::string('guid');  //標識視頻
  7         $chunks = R::numeric('chunks');  // 分片數
  8         $chunk = R::numeric('chunk');  //分片號
  9         $file_name = R::string('file_name');
 10         $file = isset($_FILES[$file_id])?$_FILES[$file_id]:'';
 11         $md5 = R::string('md5');
 12         $this->upload = new Common_Upload();
 13 
 14         if(empty($guid) || empty($file_name) || empty($md5)){
 15             $this->response_msg(-1, 'guid 或 file_name 或 md5 不能為空');
 16             return;
 17         }
 18 
 19         if(empty($file['name'])){
 20             $this->response_msg(-1, '請上傳一個文件');
 21             return;
 22         }else{
 23             if($chunks){
 24                 $res = $this->upload->saveFile_chunks($file,$chunks, $chunk, $guid);
 25                 if(empty($res)){
 26                     $this->response_msg(-2, '分片上傳失敗');
 27                     return;
 28                 }
 29 
 30             }else if($keepFileName){
 31                 $res = $this->upload->saveFile_nochunks($file, '', '', $keepFileName);
 32             }else{
 33                 $res = $this->upload->saveFile_nochunks($file);
 34             }
 35             if(empty($res)){
 36                 $err = $this->upload->getError();
 37                 $this->response_msg(-3, '上傳文件出錯!msg:'.print_r($err, true));
 38                 return;
 39             }
 40             if($unsize_change){
 41                 $size = $res['size'];
 42             }else{
 43                 $size = $this->convert_size($res['size']);
 44             }
 45 
 46             //視頻上傳完成
 47             if($chunks && $res['last_chunk']){
 48                 $domain = Kohana::$config->load('domain');
 49                 $video_domain = $domain[RUN_MOD]['VIDEO'];
 50 
 51                 if(!empty($file_name)){
 52                     $res['name'] = $file_name;
 53                 }
 54                 $video_data = array(
 55                     'video_name'=> $file_name,
 56                     'video_url'=> $video_domain."/".$res['path'],
 57                     'size'=>$res['size'],
 58                     'create_time'=> date('y-m-d H:i:s',time()),
 59                     'update_time'=> date('y-m-d H:i:s',time()),
 60                     'duration'=> $res['time'],
 61                     'md5'=>$md5
 62                 );
 63                 $video_mod = new Model_Videoinfo();
 64                 $video = $video_mod->save_video($video_data,$guid);
 65                 $res = array(
 66                     'path'=>$res['path'],
 67                     'chunks'=>$chunks,
 68                     'chunk'=>$chunk,
 69                     'size'=>$size,
 70                     'guid'=> $guid,
 71                     'video_id'=>$video[0],
 72                     'file'=>$file,
 73                     'id'=>$id
 74                 );
 75                 $this->response_msg(1,'視頻上傳成功',$res);
 76                 return;
 77             }
 78             //非分片上傳
 79             if(!$chunks){
 80                 $domain = Kohana::$config->load('domain');
 81                 $video_domain = $domain[RUN_MOD]['VIDEO'];
 82                 if(!empty($file_name)){
 83                     $res['name'] = $file_name;
 84                 }
 85                 $video_data = array(
 86                     'video_name'=> $file_name,
 87                     'video_url'=> $video_domain."/".$res['path'],
 88                     'size'=>$res['size'],
 89                     'create_time'=> date('y-m-d H:i:s',time()),
 90                     'update_time'=> date('y-m-d H:i:s',time()),
 91                     'duration'=> $res['time'],
 92                     'md5'=>$md5
 93                 );
 94                 $video_mod = new Model_Videoinfo();
 95                 $video = $video_mod->save_video($video_data,$guid);
 96                 if(empty($video)){
 97                     $this->response_msg(-6, '視頻信息保存失敗');
 98                     return;
 99                 }
100                 $res = array(
101                     'path'=>$res['path'],
102                     'video_data'=>$video_data,
103                     'size'=>$size,
104                     'guid'=> $guid,
105                     'video_id'=>$video[0],
106                     'file'=>$file,
107                     'id'=>$id
108                 );
109                 $this->response_msg(1,'視頻上傳成功',$res);
110                 return;
111             }
112             //分片上傳成功(未全部分片上傳完成)
113             $res = array(
114                 'chunks'=>$chunks,
115                 'chunk'=>$chunk,
116             );
117             $this->response_msg(2, '分片上傳成功',$res);
118         }
119     }
  1     /**
  2      * 保存分片文件(注意先驗證文件是否合法)
  3      *
  4      * @param array $file 單個文件
  5      * @param string $attachdir 上傳文件路徑
  6      * @param string $upload_type 上傳文件類型
  7      * @param bool $keepFileName 是否保持文件名,默認不不保持
  8      * @return bool
  9      */
 10     public function saveFile_chunks($file,$chunks, $chunk, $guid)
 11     {
 12         if(empty($guid) || empty($file) ){
 13             return false;
 14         }
 15         $file_name = (string)$guid . $chunk;
 16         //保存分片文件
 17         $file_info = $this->saveFile($file, '', '', false, $file_name,true);
 18         if ($file_info) {
 19             $cache = Cache::instance('memcache');
 20             //記錄已上傳的分片編號,上傳順序並不是按編號順序進行上傳
 21             $chunks_list_pre = $cache->get($guid);
 22             if(empty($chunks_list_pre)){
 23                 $strarr = array();
 24                 for($i=0;$i<$chunks;$i++){
 25                     $strarr[] = $guid.$i;
 26                 }
 27                 $cache->set($guid,$strarr,60 * 60 * 24);
 28             }
 29             $file_path = $cache->set($guid.$chunk,$file_info['path'],60 * 60 * 24);
 30 
 31             $chunk_path_array= array();
 32             for($i=0;$i<$chunks;$i++){
 33                 if($cache->get($guid.$i)){
 34                     $chunk_path_array[$i] = $cache->get($guid.$i);
 35                 }
 36             }
 37             list($Y,$M,$D,$H,$I,$S) = explode('-',date("Y-m-d-H-i-s", time()));
 38             $file_info['chunks_path_count'] = count($chunk_path_array);
 39             $file_info['last_chunk'] = false;
 40             if (count($chunk_path_array) == $chunks) {
 41                 //按目錄類型存儲
 42                 $dirType = substr($file_info['type'], 1, strlen($file_info['type']));;
 43                 //目錄類型前面加上前綴url
 44                 $dirType = $this->pre_url.$dirType;
 45                 //按年月二級存儲
 46                 $month_file_path = $Y.'/'.$M;
 47                 $saveName ='upload/mp4/'.$month_file_path.'/original/'.$guid.$file_info['type'];
 48                 $join_file_name =$this->attachDIR.$saveName;
 49                 if(!is_dir($this->attachDIR.'upload/mp4/'.$month_file_path.'/original/')){
 50                     mkdir($this->attachDIR.'upload/mp4/'.$month_file_path.'/original/',0755,true);
 51                 }
 52                 if(! file_exists($join_file_name)){
 53                     $fp = fopen($join_file_name, "ab");
 54                     //合並過程中對文件加鎖,防止同時操作而出錯
 55                     if (flock($fp,LOCK_EX)){
 56                         for ($i = 0; $i < $chunks; $i++) {
 57                                 $tmp_file = $this->attachDIR . $chunk_path_array[$i];
 58                                 $handle = fopen($tmp_file, "rb");
 59                                 fwrite($fp, fread($handle, filesize($tmp_file)));
 60                                 fclose($handle);
 61                                 unset($handle);
 62                                 unlink($tmp_file);//合並完畢的文件就刪除
 63                         }//組裝分片
 64                         $cache->delete($guid);
 65                         for($i=0;$i<$chunks;$i++){
 66                             $cache->delete($guid.$i);
 67                         }
 68                         $time = $this->getTime($join_file_name,$file_info['type']);
 69                         $file_info['time'] = $time;
 70                         $file_info['path'] = $saveName;
 71                         $file_info['size'] = filesize($join_file_name);
 72                         $file_info['last_chunk'] = true;
 73 
 74                         $model_mod = new Model_Base();
 75                         $model_mod->disconnect();
 76                         $pid = pcntl_fork();
 77                         //父進程和子進程都會執行下面代碼
 78                         if ($pid == -1) {
 79                             //錯誤處理:創建子進程失敗時返回-1.
 80                             die('could not fork');
 81                         } else if ($pid) {
 82                             $model_mod->connect();
 83                             //對上傳完成的視頻進行排隊轉碼
 84                             $this->thread($join_file_name,$file_info['type'],$guid);
 85                             //父進程會得到子進程號,所以這里是父進程執行的邏輯
 86                             pcntl_wait($status); //等待子進程中斷,防止子進程成為僵屍進程。
 87                         } else {
 88                             return $file_info;
 89                             //子進程得到的$pid為0, 所以這里是子進程執行的邏輯。
 90                         }
 91 
 92                     }
 93                 }
 94 
 95             }
 96             return $file_info;
 97         } else {
 98                 $this->error[] = '分片上傳失敗';
 99                 return false;
100         }
101             /*}}}*/
102         }

 

1,實現了分片上傳;

2,同時在上傳前檢查視頻md5 是否在庫,如已存在可實現“秒傳” 功能,即直接復制數據信息,指向同一個文件,不必再上傳;

3,可實現斷點續傳,上傳過程中中斷;之前上傳的分片已保留在服務器,只需重新上傳尚未上傳的分片即可;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM