user622897
user622897

Reputation: 19

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.

Upvotes: 1

Views: 13452

Answers (4)

ElChupacabra
ElChupacabra

Reputation: 1091

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.

Upvotes: 5

user4931222
user4931222

Reputation:

$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);

Upvotes: 0

user2065410
user2065410

Reputation: 1

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) ) {"

Upvotes: 0

AndyDeGroo
AndyDeGroo

Reputation: 91

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);
}
?>

Upvotes: 5

Related Questions