Home >Backend Development >PHP Tutorial >Detailed explanation of file splitting and merging examples of PHP breakpoint resume transfer

Detailed explanation of file splitting and merging examples of PHP breakpoint resume transfer

怪我咯
怪我咯Original
2017-07-05 10:50:421199browse

This article mainly introduces the file segmentation and merging of PHP breakpoint resume transfer in detail. It has certain reference value. Interested friends can refer to

php to implement breakpoint resume transfer. You need to split the large file into multiple small files, and then upload a single . Merge after transmission.

│ merge.php – merge file script
│ merge.zip – merged file
│ socket.zip – file that needs to be split
│ split.php – Split file script

└─split – Split small file directory

The following is the source code

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

merge.php

<?php
$filelist = glob(&#39;./split/*socket*.tmp&#39;);
$filesize = 10;

//print_r($filelist);
$mergeFileName = &#39;merg.zip&#39;;

unlink($mergeFileName);
  $fp2 = fopen($mergeFileName,"w+");
foreach($filelist as $k => $v)
{
  $fp = fopen($v, "rb");
   $content = fread($fp, $filesize);

   fwrite($fp2, $content, $filesize);
   unset($content);
   fclose($fp);
   echo $k,"\n";
}
  fclose($fp2);

The above is the detailed content of Detailed explanation of file splitting and merging examples of PHP breakpoint resume transfer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn