Created
April 25, 2013 16:27
-
-
Save caferrari/5461101 to your computer and use it in GitHub Desktop.
A simple tool to split csv files in many smaller files
This file contains hidden or 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
<?php | |
if ($argc < 2) { | |
die ('Use textsplit.php /path/do/arquivo linhas'); | |
} | |
if (!isset($argv[2])) { | |
$argv[2] = 1000; | |
} | |
list ($file, $filename, $lines) = $argv; | |
if (!file_exists($filename)) { | |
die ('Arquivo de destino não encontrado'); | |
} | |
if (!is_numeric($lines) || $lines < 0) { | |
die ('Linhas deve ser numerico'); | |
} | |
$file = fopen($filename, 'r'); | |
$outputFilename = explode('.', basename($filename)); | |
$workedLines = 0; | |
$fileID = 0; | |
while (false !== $line = fgets($file) ) { | |
file_put_contents($outputFilename[0] . "_{$fileID}." . $outputFilename[1], $line, FILE_APPEND); | |
if (++$workedLines % $lines == 0) { | |
$fileID++; | |
} | |
} | |
fclose($file); | |
echo "DONE" . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment