<?php
for($i=1; $i<=5; $i++){
for($j=1; $j<=100000; $j++){
file_put_contents("test/$i.txt", rand(100,999) . PHP_EOL, FILE_APPEND);
}
}<?php
// [0-9].txt başlangıç dosyası (1.txt)
$fFirst = 1;
// [0-9].txt sayısını belirler (1.txt, 2.txt, 3.txt..)
$fCount = 5;
// Dosyalar hangi dizinde ?
$d = "./test";
// Çıktı dosyasının adı
$fOutput = $d . DIRECTORY_SEPARATOR . "cikti.txt";
if(!file_exists($d)){
exit("Dizin bulunamadi!");
}
$afHandle = array();
for($i = 1; $i<=$fCount; $i++){
$p = $d . DIRECTORY_SEPARATOR . $i . ".txt";
if(file_exists($p)) {
$afHandle[$i] = fopen($p, "r");
if(!$afHandle[$i]) {
exit("$i.txt dosyasi acilamadi!");
}
}
}
while(!feof($afHandle[$fFirst])){
for($i = 1; $i<=$fCount; $i++){
file_put_contents($fOutput, rtrim(fgets($afHandle[$i])), FILE_APPEND); // Memory <3
}
file_put_contents($fOutput, PHP_EOL, FILE_APPEND);
usleep(50); // CPU <3
}
exit("OK");5 adet 100k satır içeren test dosyası oluşturdum ardından tüm dosyaların satırlarını birleştirdim, %1 işlemci kullanımı ve sabit memory kullanımı ile işlem tamamlandı.