Home >Backend Development >PHP Tutorial >Detailed explanation of file splitting and merging examples of PHP breakpoint resume transfer
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('./split/*socket*.tmp'); $filesize = 10; //print_r($filelist); $mergeFileName = 'merg.zip'; 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!