php 视频 切片上传 秒传

1. 简介 
Plupload是不支持断点续传的,但是他支持分片上传,因此我们只需要打开其分片上传后,在文件上传之前请求一个服务器当前文件已上传的大小,
如果上传大小为0则认为没有上传过,重新上传,如果上传文件大小大于0则从已上传的切片的下一个切片开始上传。 

Plupload分片上传使用了html5的新特性,针对ie8 ie9等不支持html5的情况,采用flash等进行分片上传,
但是plupload不建议采用flash分片上传,因为flash在分片上传读取待上传文件时不能读取一部分,只能将文件全部加载到内存中再进行分片上传,会消耗客户端内存。
因此默认情况没有打开分片上传,而且根据他官方给的强制打开方法也不好使.

1
<!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 5 <script type="text/javascript" src="../js/plupload.full.min.js"></script> 6 </head> 7 8 <body style="font: 13px Verdana; background: #eee; color: #333"> 9 10 <h1>断点上传</h1> 11 <p>Shows you how to use the core plupload API.</p> 12 <div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div> 13 <br/> 14 15 <div id="container"> 16 <a id="pickfiles" href="javascript:;">[选择文件]</a> 17 <a id="uploadfiles" href="javascript:;">[上传文件]</a> 18 </div> 19 20 <br/> 21 <pre id="console"></pre> 22 23 <script type="text/javascript"> 24 25 var uploader = new plupload.Uploader({ 26 runtimes: 'html5,flash,silverlight,html4', 27 browse_button: 'pickfiles', // you can pass an id... 28 container: document.getElementById('container'), // ... or DOM Element itself 29 url: 'upload.php', 30 flash_swf_url: '../js/Moxie.swf', 31 silverlight_xap_url: '../js/Moxie.xap', 32 chunk_size: '2000kb', //分片上传文件时,每片文件被切割成的大小,为数字时单位为字节 33 max_retries: 100, //当发生plupload.HTTP_ERROR错误时的重试次数,为0时表示不重试 34 filters: { 35 max_file_size: '10G', 36 mime_types: [ 37 {title: "Image files", extensions: "jpg,gif,png"}, 38 {title: "Zip files", extensions: "zip"}, 39 {title: "Video Files", extensions: "dat,asf,rm,ram,3gp,mov,m4v,dvix,dv,qt,divx,cpk,fli,flc,mod,mp4,wmv,flv,avi,mkv,vob,mpg,rmvb,mpeg,mov,mts"} 40 41 ], 42 multi_selection: true, 43 }, 44 45 init: { 46 PostInit: function () { 47 document.getElementById('filelist').innerHTML = ''; 48 49 document.getElementById('uploadfiles').onclick = function () { 50 uploader.start(); 51 return false; 52 }; 53 }, 54 55 FilesAdded: function (up, files) { 56 plupload.each(files, function (file) { 57 document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>'; 58 }); 59 }, 60 61 UploadProgress: function (up, file) { 62 document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>"; 63 }, 64 65 Error: function (up, err) { 66 document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message)); 67 } 68 } 69 }); 70 71 uploader.init(); 72 </script> 73 </body> 74 </html>

 



<php部分>

  1 <?php
  2 /**
  3  * upload.php
  4  *
  5  * Copyright 2013, Moxiecode Systems AB
  6  * Released under GPL License.
  7  *
  8  * License: http://www.plupload.com/license
  9  * Contributing: http://www.plupload.com/contributing
 10  */
 11 
 12 #!! IMPORTANT: 
 13 #!! this file is just an example, it doesn't incorporate any security checks and 
 14 #!! is not recommended to be used in production environment as it is. Be sure to 
 15 #!! revise it and customize to your needs.
 16 
 17 
 18 // Make sure file is not cached (as it happens for example on iOS devices)
 19 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 20 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 21 header("Cache-Control: no-store, no-cache, must-revalidate");
 22 header("Cache-Control: post-check=0, pre-check=0", false);
 23 header("Pragma: no-cache");
 24 
 25 /* 
 26 // Support CORS
 27 header("Access-Control-Allow-Origin: *");
 28 // other CORS headers if any...
 29 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
 30    exit; // finish preflight CORS requests here
 31 }
 32 */
 33 
 34 // 5 minutes execution time
 35 @set_time_limit(5 * 60);
 36 
 37 // Uncomment this one to fake upload time
 38 // usleep(5000);
 39 
 40 // Settings
 41 $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
 42 //$targetDir = 'uploads';
 43 $cleanupTargetDir = true; // Remove old files
 44 $maxFileAge = 5 * 3600; // Temp file age in seconds
 45 
 46 
 47 error_log(var_export($_REQUEST,true),3,'/home/zhouyu/txt.log');
 48 
 49 // Create target dir
 50 if (!file_exists($targetDir)) {
 51    @mkdir($targetDir);
 52 }
 53 
 54 // Get a file name
 55 if (isset($_REQUEST["name"])) {
 56    $fileName = $_REQUEST["name"];
 57 } elseif (!empty($_FILES)) {
 58    $fileName = $_FILES["file"]["name"];
 59 } else {
 60    $fileName = uniqid("file_");
 61 }
 62 
 63 $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
 64 
 65 // Chunking might be enabled
 66 $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
 67 $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
 68 
 69 
 70 // Remove old temp files   
 71 if ($cleanupTargetDir) {
 72    if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
 73       die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
 74    }
 75 
 76    while (($file = readdir($dir)) !== false) {
 77       $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
 78 
 79       // If temp file is current file proceed to the next
 80       if ($tmpfilePath == "{$filePath}.part") {
 81          continue;
 82       }
 83 
 84       // Remove temp file if it is older than the max age and is not the current file
 85       if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
 86          @unlink($tmpfilePath);
 87       }
 88    }
 89    closedir($dir);
 90 }  
 91 
 92 
 93 // Open temp file
 94 if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
 95    die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
 96 }
 97 
 98 if (!empty($_FILES)) {
 99    if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
100       die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
101    }
102 
103    // Read binary input stream and append it to temp file
104    if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
105       die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
106    }
107 } else {   
108    if (!$in = @fopen("php://input", "rb")) {
109       die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
110    }
111 }
112 
113 while ($buff = fread($in, 4096)) {
114    fwrite($out, $buff);
115 }
116 
117 @fclose($out);
118 @fclose($in);
119 
120 // Check if file has been uploaded
121 if (!$chunks || $chunk == $chunks - 1) {
122    // Strip the temp .part suffix off 
123    rename("{$filePath}.part", $filePath);
124 }
125 
126 // Return Success JSON-RPC response
127 die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');

 



转载于:https://www.cnblogs.com/lingwu/p/9361083.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值