您的位置:首页 > 编程语言 > PHP开发

分片上传,断点续传,php文件操作,

2017-10-11 16:35 267 查看
 分片上传:前台把文件切割成多个文件,分多次上传,携带文件总个数和当前文件编号的参数。后台确认全部接受后合并。

 断点续传:前台上传文件(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, '又暂停了');
}


        

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: