Method of Merging Files Out of Order by Breakpoint Continuing Transfer in PHP

  • 2021-11-02 00:10:03
  • OfStack

In this paper, an example is given to describe the method of merging files out of order by PHP. Share it for your reference, as follows:

Split into multiple files to send, due to network reasons, it can be sent and received without sending first. So we can't merge in sequence.

Split file source code the first article "PHP Breakpoint Continued File Split Merge"

merge2.php


<?php
$fileTotaiSize = filesize("socket.zip");
$filelist = glob('./split/*socket*.tmp');
$filesize = 3096;
// Disturb the order of reading files 
shuffle($filelist);
$mergeFileName = 'merg2.zip';
 if(is_file($mergeFileName))
 {
 unlink($mergeFileName);
 }
 $fp2 = fopen($mergeFileName,"w+");
 // Create a blank file 
 $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);

Add: The split file split. php in the previous article is as follows:


<?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);

More readers interested in PHP can check the topics of this site: "Summary of PHP Directory Operation Skills", "Summary of php File Operation", "Summary of PHP Common Traversal Algorithms and Skills", "Tutorial on PHP Data Structure and Algorithms", "Summary of php Programming Algorithms", "Summary of php socket Usage" and "Summary of PHP Network Programming Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: