Skip to content

Instantly share code, notes, and snippets.

@kadko
Last active February 14, 2020 17:36
Show Gist options
  • Save kadko/055c9ebb20c2a7cac904d9dc830356d1 to your computer and use it in GitHub Desktop.
Save kadko/055c9ebb20c2a7cac904d9dc830356d1 to your computer and use it in GitHub Desktop.
Outputs file to small parts as keeping original file data format
<?php
class splitFile{
private $fileName;
private $filecontents;
private $fileLen;
private $PIN;
private $partSize;
function __construct($fileName, $outputFileName, $pin, $partsize) {
$this->fileName = $fileName;
$this->outputFileName = $outputFileName;
$this->PIN = $pin;
$this->partSize = ceil($partsize);
$this->filecontents = file_get_contents($this->fileName);
$this->fileLen = mb_strlen($this->filecontents);
}
function split(){
$fileContents = $this->filecontents;
$PINlen = mb_strlen( $this->PIN );
$len = $this->fileLen / $this->partSize;
$past = 0;
for($i=1; $i < $len; $i++){
$splitpos = mb_strpos($fileContents, $this->PIN, $i * $this->partSize);
if( $i == floor($len) ){
$part = mb_substr($fileContents, $past + $PINlen );
}else{
if($i != 1){ $marginS = $PINlen; $marginE = 0; }else{ $marginS = 0; $marginE = $PINlen; }
$part = mb_substr($fileContents, $past + $marginS, $splitpos - $past + $marginE );
}
$past = $splitpos;//get previous last as current start offset
file_put_contents($this->outputFileName . $i . '.csv', $part);
}
return $this->outputResult();
}
function outputResult(){
$output = 'Completed!';
$output .= '<br>';
$output .= 'Master file size (MB):'. round($this->fileLen/(1024*1024), 1);
$output .= '<br>';
$output .= 'Total part qty: ' . floor($this->fileLen / $this->partSize);
$output .= '<br>';
$output .= 'Part files average size(MB): ' . round($this->partSize/(1024*1024), 2);
return Array('output' => $output, 'parts' => floor($this->fileLen / $this->partSize));
}
}
?>
@kadko
Copy link
Author

kadko commented Dec 2, 2019

// example for splitting SQL file: part size 1MB / 5
$splitter = new splitFile('sampleSQL.txt', ');', (1024*1024) / 5 );
$splitter->split();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment