Skip to content

Instantly share code, notes, and snippets.

@caferrari
Created April 25, 2013 16:27
Show Gist options
  • Save caferrari/5461101 to your computer and use it in GitHub Desktop.
Save caferrari/5461101 to your computer and use it in GitHub Desktop.
A simple tool to split csv files in many smaller files
<?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