您好,欢迎访问一九零五行业门户网

php怎么上传视频大文件

php上传视频大文件的方法:首先传入文件对象,并通过【cutfile】方法切割文件;然后使用【sendfile】方法发送文件;接着通过【filemerge】方法进行文件合成;最后返回数据即可实现上传视频大文件功能。
php怎么上传视频大文件?
php+上传视频大文件
理清思路:
引入了两个概念:块(block)和片(chunk)。每个块由一到多个片组成,而一个资源则由一到多个块组成
块是服务端的永久数据存储单位,片则只在分片上传过程中作为临时存储的单位。服务端会以约一个月为单位周期性的清除上传后未被合并为块的数据片
实现过程:
将文件分割,分片上传,然后合并
前端核心code:
var fileform = document.getelementbyid("file"); var upstartbtn = document.getelementbyid('upstart'); var stopbtn = document.getelementbyid('stop'); var startbtn = document.getelementbyid('restart'); var rate = document.getelementbyid('rate'); var divlog = document.getelementbyid('divlog'); //--------------------------- const length = 1024 * 1024 * 1; var start = 0; var end = start + length; var blob; var blob_num = 1; var is_stop = 0 var file = null; var md5filename = ''; //----------------------------- var upload_instance = new upload(); fileform.onchange = function() { browsermd5file(fileform.files[0], function (err, md5) { //如果文件大,md5值生成较慢 md5值生成后才能上传处理,自己优化下吧 md5filename = md5; //如果需要刷新后也能断点,可利用cookie记录,自行完善 divlog.innerhtml = '文件md5为:' + md5filename; }); } upstartbtn.onclick = function(){ upload_instance.addfileandsend(fileform); } stopbtn.onclick = function(){ upload_instance.stop(); } startbtn.onclick = function(){ upload_instance.start(); } function upload(){ var xhr = new xmlhttprequest(); var form_data = new formdata(); //对外方法,传入文件对象 this.addfileandsend = function(that){ file = that.files[0]; blob = cutfile(file); sendfile(blob,file); blob_num += 1; } //停止文件上传 this.stop = function(){ xhr.abort(); is_stop = 1; } this.start = function(){ sendfile(blob,file); is_stop = 0; } //切割文件 function cutfile(file){ var file_blob = file.slice(start,end); start = end; end = start + length; return file_blob; }; //发送文件 function sendfile(blob,file){ var total_blob_num = math.ceil(file.size / length); form_data.append('file',blob); form_data.append('blob_num',blob_num); form_data.append('total_blob_num',total_blob_num); form_data.append('md5_file_name',md5filename); form_data.append('file_name',file.name); xhr.open('post','./index.php',false); xhr.onreadystatechange = function () { var progress; var progressobj = document.getelementbyid('finish'); if(total_blob_num == 1){ progress = '100%'; }else{ progress = (math.min(100,(blob_num/total_blob_num)* 100 )).tofixed(2) +'%'; } console.log('progress-----'+progress); progressobj.style.width = progress; rate.innerhtml = progress; var t = settimeout(function(){ if(start < file.size && is_stop === 0){ blob = cutfile(file); sendfile(blob,file); blob_num += 1; }else{ //settimeout(t); } },1000); } xhr.send(form_data); } }
后端code
<?php class upload{ private $filepath = './upload'; //上传目录 private $tmppath; //php文件临时目录 private $blobnum; //第几个文件块 private $totalblobnum; //文件块总数 private $filename; //文件名 private $md5filename; public function __construct($tmppath,$blobnum,$totalblobnum,$filename, $md5filename){ $this->tmppath = $tmppath; $this->blobnum = $blobnum; $this->totalblobnum = $totalblobnum; $this->filename = $this->createname($filename, $md5filename); $this->movefile(); $this->filemerge(); } //判断是否是最后一块,如果是则进行文件合成并且删除文件块 private function filemerge(){ if($this->blobnum == $this->totalblobnum){ $blob = ''; for($i=1; $i<= $this->totalblobnum; $i++){ $blob .= file_get_contents($this->filepath.'/'. $this->filename.'__'.$i); } file_put_contents($this->filepath.'/'. $this->filename,$blob); $this->deletefileblob(); } } //删除文件块 private function deletefileblob(){ for($i=1; $i<= $this->totalblobnum; $i++){ @unlink($this->filepath.'/'. $this->filename.'__'.$i); } } private function movefile(){ $this->touchdir(); $filename = $this->filepath.'/'. $this->filename.'__'.$this->blobnum; move_uploaded_file($this->tmppath,$filename); } //api返回数据 public function apireturn(){ if($this->blobnum == $this->totalblobnum){ if(file_exists($this->filepath.'/'. $this->filename)){ $data['code'] = 2; $data['msg'] = 'success'; $data['file_path'] = 'http://'.$_server['http_host'].dirname($_server['document_uri']).str_replace('.','',$this->filepath).'/'. $this->filename; } }else{ if(file_exists($this->filepath.'/'. $this->filename.'__'.$this->blobnum)){ $data['code'] = 1; $data['msg'] = 'waiting'; $data['file_path'] = ''; } } header('content-type: application/json'); echo json_encode($data); } private function touchdir(){ if(!file_exists($this->filepath)){ return mkdir($this->filepath); } } private function createname($filename, $md5filename){ return $md5filename . '.' . pathinfo($filename)['extension']; }} $upload = new upload($_files['file']['tmp_name'],$_post['blob_num'],$_post['total_blob_num'],$_post['file_name'],$_post['md5_file_name']); $upload->apireturn();
效果展示:
更多相关技术文章,请访问!
其它类似信息

推荐信息