本文实例讲述了PHP实现断点续传乱序合并文件的方法。分享给大家供大家参考,具体如下:
分割成多个文件发送,由于网络原因并不上先发就能发接收到。所以我们不能按顺序合并。
分割文件源码前面一篇文章《PHP断点续传之文件分割合并》
merge2.php
<?php $fileTotaiSize = filesize("socket.zip"); $filelist = glob('./split/*socket*.tmp'); $filesize = 3096; //打乱读取文件顺序 shuffle($filelist); $mergeFileName = 'merg2.zip'; if(is_file($mergeFileName)) { unlink($mergeFileName); } $fp2 = fopen($mergeFileName,"w+"); //创建空白文件 $str = str_pad('',$fileTotaiSize); fwrite($fp2, $str, $fileTotaiSize); foreach($filelist as $k => $v) { $tmp = explode('.',$v); $len = explode('-', $tmp[3]); $offset = (int) $len[0]; $fp = fopen($v, "rb"); $content = fread($fp, $filesize); echo $offset, "\n"; fseek($fp2, $offset); fwrite($fp2, $content, strlen($content)); unset($content); fclose($fp); } fclose($fp2);
补充:前文中的分割文件split.php如下:
<?php $fp = fopen("socket.zip", "rb"); $filesize = 10; $i = 0; $no = 1; while(!feof($fp)) { $file = fread($fp, $filesize); $fp2 = fopen("./split/socket.port".sprintf("%04d",$no).".".$i."-".($i+$filesize).".tmp", "wb"); fwrite($fp2, $file, $filesize); fclose($fp2); $i+=$filesize+1; $no++; } fclose($fp);
希望本文所述对大家PHP程序设计有所帮助。
本文链接:https://my.lmcjl.com/post/15224.html
展开阅读全文
4 评论