-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added splitter to split way large files
- Loading branch information
1 parent
e1eaff2
commit 0ac4849
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: tarunjangra | ||
* Date: 18/12/14 | ||
* Time: 18:38 | ||
*/ | ||
|
||
namespace Importer; | ||
|
||
|
||
class Splitter { | ||
private $config = null; | ||
|
||
public function __construct(&$config){ | ||
$this->config = $config; | ||
} | ||
|
||
public function split($size = 90000000){ | ||
$done = false; | ||
$part = 0; | ||
$filename = basename($this->config->source_file,'.csv'); | ||
if (($handle = fopen($this->config->source_file, "r")) !== FALSE) { | ||
$header = fgets($handle); | ||
while ($done == false) { | ||
$locA = ftell($handle); // gets the current location. START | ||
fseek($handle, $size, SEEK_CUR); // jump the length of $size from current position | ||
$tmp = fgets($handle); // read to the end of line. We want full lines | ||
$locB = ftell($handle); // gets the current location. END | ||
$span = ($locB - $locA); | ||
fseek($handle, $locA, SEEK_SET); // jump to the START of this chunk | ||
$chunk = fread($handle,$span); // read the chunk between START and END | ||
$destination = $this->config->workarea_root.date('Y/m/d').DIRECTORY_SEPARATOR.$this->config->workarea.DIRECTORY_SEPARATOR.$filename; | ||
file_put_contents($destination.'_'.$part.'.csv', $header.$chunk); | ||
$part++; | ||
if (strlen($chunk) < $size) $done = true; | ||
} | ||
fclose($handle); | ||
} | ||
} | ||
|
||
} |