2022年7月26日星期二

[PHP] split a large file to parts and merge them back to a file

//split 

$filename = 'bigzip.zip';//in public folder
$MBperPart = 45;

$handle1 = fopen($filename, 'r');
$kbcount = 0;
$part = 0;
$handle2 = fopen($filename.'_part_'.$part, 'w');
while (!feof($handle1)) {
$data = fread($handle1, 1024);
fwrite($handle2, $data, strlen($data));
$kbcount++;
if($kbcount == 1024*$MBperPart){
fclose($handle2);
$part++;
$handle2 = fopen($filename.'_part_'.$part, 'w');
$kbcount = 0;
}
}

fclose($handle1);
fclose($handle2);




//merge

$filename = 'bigzip.zip'; //bigzip.zip_part_0 , bigzip.zip_part_1 , bigzip.zip_part_2
//rightclick Download > Copy Link Address at GH
$partnum = 10; //if 10 -> 0,1,2,3,4,5,6,7,8,9


header('Content-Disposition: attachment; filename="' . end(explode('/',$filename)) . '"');
//define downloaded file name for client

for ($i = 0; $i < $partnum; $i++) {
$handle1 = fopen($filename . '_part_'.$i, 'rb');
while (!feof($handle1)) {
$data = fread($handle1, 1024);
echo $data;

flush(); //output to client browser and clear buffer (free RAM)
ob_flush(); //output to client browser and clear buffer (free RAM)
}
fclose($handle1);
}

沒有留言:

發佈留言

[Fixed] some windows chrome, scrolling has glitch position not accurate

   [Fixed] some windows chrome, scrolling has glitch position not accurate Smooth Scrolling Flag 📜 Chrome has an experimental "smooth ...