Jump to content

[SOLVED] generate random file


Tredan

Recommended Posts

I need to generate a file named randomly with 9 characters for example 123456789.reg except the numbers should be random.

I tried this but i geuss i dont understand rand() well enough.

<?php
  
$1 = (rand()%9);
$2 = (rand()%9);
$3 = (rand()%9);
$4 = (rand()%9);
$5 = (rand()%9);  
$6 = (rand()%9);
$7 = (rand()%9);
$8 = (rand()%9);
$9 = (rand()%9);
$myFile = "regcodes/" . $1 . $2 . $3 . $4 . $5 . $6 . $7 . $8 . $9 . ".reg"
$fh = fopen($myFile, 'w');
fwrite($fh, "1");
fclose($fh);
?>


Link to comment
https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/
Share on other sites

for() loop.

 

// Generate N empty files

$num_of_files = abs(intval($_GET['files']));

if ($num_of_files) {
$count = 0;
while ($count < $num_of_files) {
	$filename = sprintf('regcodes/%09d.reg',mt_rand(0,999999999));
	if (file_exists($filename)) continue;
	fclose(fopen($filename,'w'));
	$count++;
}
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.