Split a file by a set number of lines

I had large files that I had to split into smaller files, 
all the examples I found ran out of memory when running. 
I created this class which can split 100 MB files easily and quickly. 
Let me know if you find it useful.
class SplitFile {

    /*
     * @var Full path or relative path to where the output file will be created
     */
    private $_dest_path;

    /*
     * __construct
     */
    public function __construct($dest_path = '.')
    {
        $this->_dest_path = $dest_path;
    }

    /*
     * Split the input (text) file into smaller files by number of lines
     *
     * @param string $source_file File path of the source text file
     * @param string $output_prefix File prefix for the output files
     * @param int $split_count Number of lines per split file
     */
    public function split($source_file, $output_prefix = '', $split_count = '500000')
    {
        // File is less then split count so return file
        if($this->_getFileLineCount($source_file) <= $split_count) {
            return $this->_dest_path . '/' . $source_file;
        }

        $fileExtension = pathinfo($source_file,PATHINFO_EXTENSION);

        if($output_prefix == '') {
            $output_prefix = basename($source_file,'.'.$fileExtension);
        }

        $fileExtension = pathinfo($source_file,PATHINFO_EXTENSION);

        // Open the file for reading
        $handle = fopen($source_file, "r");

        $lineCount = 0;
        $split = 1;

        // Loop through the file until you reach the last line
        while(!feof($handle)) {
            if ($lineCount == 0) {
                $outfile = $this->_dest_path . '/' . $output_prefix . '-' . $split . ".$fileExtension";
                $outHandle = fopen($outfile,'w+');
            }

            fwrite($outHandle, fgets($handle));

            if($lineCount < $split_count) {
                // Increment the counter
                $lineCount++;
            } else {
                $split++;
                $lineCount = 0;
            }
        }

        // Release the file for access
        fclose($handle);
        unlink($source_file);
    }

    /**
    * Get the line count of a file
    *
    * @param string $file The file name
    * @return int The number of lines in a file
    */
    private function _getFileLineCount($file)
    {
        // Open the file for reading
        $handle = fopen($file, "r");
        $lineCount = 0;

        // Loop through the file until you reach the last line
        while (fgets($handle) !== false) {
            // Increment the counter
            $lineCount++;
        }

        // Release the file for access
        fclose($handle);
        return $lineCount;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s