分片上传,断点续传,php文件操作,
2017-10-11 16:35
267 查看
分片上传:前台把文件切割成多个文件,分多次上传,携带文件总个数和当前文件编号的参数。后台确认全部接受后合并。
断点续传:前台上传文件(http状态码206),中途取消,后台对已经上传的部分进行保存,当前台第二次上传时在对文件进行续写。
分割文件的方法:
合并文件的方法:
续写的方法:
断点续传:前台上传文件(http状态码206),中途取消,后台对已经上传的部分进行保存,当前台第二次上传时在对文件进行续写。
分割文件的方法:
<?php $i = 0; //分割的块编号 $fp = fopen("2.mp4","rb"); //要分割的文件 $file = fopen("split_hash.txt","a"); //记录分割的信息的文本文件,实际生产环境存在redis更合适 echo filesize($fp); while(!feof($fp)){ $handle = fopen("2.{$i}","wb"); fwrite($handle,fread($fp,52428));//切割的块大小 5m fwrite($file,"2.{$i}\r\n"); fclose($handle); unset($handle); $i++; } fclose ($fp); fclose ($file); echo "ok";
合并文件的方法:
<?php $hash = file_get_contents("split_hash.txt"); //读取分割文件的信息 $list = explode("\r\n",$hash); $fp = fopen("2.jpg","ab"); //合并后的文件名 foreach($list as $value){ if(!empty($value)) { $handle = fopen($value,"rb"); fwrite($fp,fread($handle,filesize($value))); fclose($handle); unset($handle); } } fclose($fp); echo "ok";
续写的方法:
//文件是否 存在 if(file_exists($folder_path)){ //对文件的续写操作 $tmp_flg = file_put_contents($file_path, file_get_contents($file['tmp_name']),FILE_APPEND); }else{ //创建文件夹 $fileUtil->createDir($folder_path); //移动到磁盘 $info = $images->validate(['size'=>__SPLIT_SIZE__,'ext'=>__IMAGES_TYPE__.__VIDEO_TYPE__])->rule('uniqid')->move($folder_path,''); //是否成功 if(!$info){ return $this->_json_return(false, $images->getError()); } } //大小比对 if(filesize($file_path)<$split_size){ return $this->_json_return(false, '又暂停了'); }
相关文章推荐
- php操作ftp上传文件、创建目录、删除文件、删除目录、下载文件到本地
- PHP文件操作(三)PHP实现文件上传和下载
- PHP文件上传操作实例详解
- php操作ftp上传文件、创建目录、删除文件、删除目录、下载文件到本地
- php 应用 bootstrap-fileinput 上传文件 插件 操作的方法
- php 应用 bootstrap-fileinput 上传文件 插件 操作的方法
- php 的文件操作,上传,中常遇到的问题
- PHP 安全手册 第七条 文件上传操作进行规避一些风险
- PHP学习笔记--文件目录操作(文件上传实例)
- php文件操作 复制 上传
- thnkphp文件上传操作
- php学习基础-文件系统(三) 文件夹的相关操作、php文件上传、php文件下载
- PHP文件上传操作实例详解(包含上传文件类型MIME验证)
- 三种方法实现php网络请求(表单操作文件上传)
- PHP多文件上传操作
- PHP文件上传和文件操作案例
- cocos2dx上传文件至php服务器操作
- PHP多文件上传操作
- PHP 文件操作2 上传下载
- php课程---文件操作及文件上传的代码总结