Skip to content

Instantly share code, notes, and snippets.

@wilon
Created May 18, 2017 02:36
Show Gist options
  • Save wilon/9ea0993d4cac1ae060d58a58e52ff426 to your computer and use it in GitHub Desktop.
Save wilon/9ea0993d4cac1ae060d58a58e52ff426 to your computer and use it in GitHub Desktop.
Files were randomly assigned into n small files.
<?php
$fileName = __DIR__ . '/file.txt';
$fp = fopen($fileName, 'r');
while(!feof($fp)) {
$buffer = fgets($fp, 4096);
$k = mt_rand(0, 9);
$res[$k][] = $buffer;
}
fclose($fp);
foreach ($res as $k => $v) {
shuffle($v);
$fileStr = implode("", $v);
file_put_contents(__DIR__ . "/file_$k.txt", $fileStr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment