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相关内容感兴趣的读者可查看本站专题:《PHP目录操作技巧汇总》、《php文件操作总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php socket用法总结》及《PHP网络编程技巧总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/494559ac6b8d34b5a7489320f886a127.html