Created
March 4, 2015 02:05
-
-
Save lesstif/ee5c6066280877be88d8 to your computer and use it in GitHub Desktop.
Quickly generate large text file with random string content.
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
#!/usr/bin/env php | |
<?php | |
## http://stackoverflow.com/questions/4356289/php-random-string-generator | |
function generateRandomString($length = 10) | |
{ | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, $charactersLength - 1)]; | |
} | |
return $randomString; | |
} | |
/** | |
* Create text file | |
* | |
* @param string $filename output file name | |
* @param int $size ouput file size | |
* @param boolean $force | |
*/ | |
function createLargeTextFile($filename, $size, $force = false) | |
{ | |
if (file_exists($filename) && $force == false) | |
{ | |
die("Output file '$filename' is exist!.\n\n"); | |
} | |
$fp = fopen($filename, 'w'); | |
for ($i = 0; $i < $size; $i++) | |
{ | |
fwrite($fp, generateRandomString(1024)); | |
fwrite($fp, "\n"); | |
} | |
fclose($fp); | |
} | |
// | |
$shortopts = ""; | |
$shortopts .= "f:"; // output file name. | |
$shortopts .= "o"; // if output file exist it will be overwritten. | |
$shortopts .= "s:"; // output file size(kilo byte). default 10 | |
$options = getopt($shortopts); | |
$file = array_key_exists('f', $options) ? $options['f'] : 'data.txt'; | |
$size = array_key_exists('s', $options) ? intval($options['s']) : 10; | |
$force = array_key_exists('o', $options) ? true : false; | |
#var_dump($options); | |
createLargeTextFile($file, $size, $force); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment