开发者

PHP script to split large text file into multiple files

I am struggling to create a PHP script to help开发者_如何学运维 split a large text file into multiple smaller files based on number of lines. I need the option to increment the split, so it starts with 10 lines on the first file, 20 lines on the second and so on.


Here is one function from my scripts:

<?php
/**
 *
 * Split large files into smaller ones
 * @param string $source Source file
 * @param string $targetpath Target directory for saving files
 * @param int $lines Number of lines to 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 to file ($fname)";
                exit;
            }
            fclose($fhandle);
            $j++;
            $buffer='';
            $i=0;
            $line+=10; // add 10 to $lines after each iteration. Modify this line as required
        }
    }
    fclose ($handle);
}
?>


It should be a comment but I cannot comment yet. AndyDeGroo's answer is wrong. It won't write last lines of file or won't save file when below 10 lines. File should be saved also AFTER while() loop to write the rest of the file. Fixed function should look like that:

<?php
/**
 *
 * Split large files into smaller ones
 * @param string $source Source file
 * @param string $targetpath Target directory for saving files
 * @param int $lines Number of lines to 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";
            saveToFile($buffer, $fname);
            $j++;
            $i=0;
        }
    }
    $fname = $targetpath.".part_".$date.$j.".log";
    saveToFile($buffer, $fname);
    fclose ($handle);
}

function saveToFile(&$buffer, $fname)
{
    if (!$fhandle = @fopen($fname, 'w')) {
        echo "Cannot open file ($fname)";
        exit;
    }
    if (!@fwrite($fhandle, $buffer)) {
        echo "Cannot write to file ($fname)";
        exit;
    }
    fclose($fhandle);
    $buffer = '';
}
?>

PS. I removed "$line" variable because it wasn't used anywhere.


Hmm. don't you need to include the last part of the file? I would like to replace "if ($i >= $lines) {" with "if ($i >= $lines || feof ($handle) ) {"


$handle = fopen('source/file/path','r'); 
        $f = 1; //new file number
        while(!feof($handle))
        {
            $newfile = fopen('new/file/path'.$f.'.txt','w'); //create new file to write to with file number
            for($i = 1; $i <= 5000; $i++) //for 5000 lines
            {
                $import = fgets($handle);
                //print_r($import);
                fwrite($newfile,$import);
                if(feof($handle))
                {break;} //If file ends, break loop
            }
            fclose($newfile);

            $f++; //Increment newfile number
        }
        fclose($handle);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜