PHP script to split large text file into multiple files -
i struggling create php script split large text file multiple smaller files based on number of lines. need option increment split, starts 10 lines on first file, 20 lines on second , on.
here 1 function scripts:
<?php /** * * split large files smaller ones * @param string $source source file * @param string $targetpath target directory saving files * @param int $lines number of lines split * @return void */ function split_file($source, $targetpath='./logs/', $lines=10){ $i=0; $j=1; $date = date("m-d-y"); $buffer=''; $handle = @fopen ($source, "r"); while (!feof ($handle)) { $buffer .= @fgets($handle, 4096); $i++; if ($i >= $lines) { $fname = $targetpath.".part_".$date.$j.".log"; if (!$fhandle = @fopen($fname, 'w')) { echo "cannot open file ($fname)"; exit; } if (!@fwrite($fhandle, $buffer)) { echo "cannot write file ($fname)"; exit; } fclose($fhandle); $j++; $buffer=''; $i=0; $line+=10; // add 10 $lines after each iteration. modify line required } } fclose ($handle); } ?>
Comments
Post a Comment