Tredan Posted June 4, 2007 Share Posted June 4, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/ Share on other sites More sharing options...
chigley Posted June 4, 2007 Share Posted June 4, 2007 <?php $rand = rand (111111111, 999999999); $myFile = "regcodes/{$rand}.reg"; $fh = fopen($myFile, 'w'); fwrite($fh, "1"); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267840 Share on other sites More sharing options...
Tredan Posted June 4, 2007 Author Share Posted June 4, 2007 <?php $rand = rand (111111111, 999999999); $myFile = "regcodes/{$rand}.reg"; $fh = fopen($myFile, 'w'); fwrite($fh, "1"); fclose($fh); ?> Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267842 Share on other sites More sharing options...
Wildbug Posted June 4, 2007 Share Posted June 4, 2007 Does it need to be that format? If not, check out tempnam() Otherwise, there's this: $myFile = sprintf('regcodes%09d.reg',mt_rand(0,999999999)); Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267843 Share on other sites More sharing options...
Psycho Posted June 4, 2007 Share Posted June 4, 2007 That sprintf statemetn is the same as I was posting, but it needs a forward splash between the directory name and file name. Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267844 Share on other sites More sharing options...
Tredan Posted June 4, 2007 Author Share Posted June 4, 2007 ok how would i have it so i could goto the gen.php file and enter say this gen.php?files=10 and it would create 10 files? with that code that chigley posted i know this much $get=$_GET["files"]; Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267853 Share on other sites More sharing options...
chigley Posted June 4, 2007 Share Posted June 4, 2007 <?php for ($i = 1; $i <= $_GET["files"]; $i++) { $rand = rand (111111111, 999999999); $myFile = "regcodes/{$rand}.reg"; $fh = fopen($myFile, 'w'); fwrite($fh, "1"); fclose($fh); echo "Created file: {$myFile}<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267856 Share on other sites More sharing options...
Tredan Posted June 4, 2007 Author Share Posted June 4, 2007 <?php for ($i = 1; $i <= $_GET["files"]; $i++) { $rand = rand (111111111, 999999999); $myFile = "regcodes/{$rand}.reg"; $fh = fopen($myFile, 'w'); fwrite($fh, "1"); fclose($fh); echo "Created file: {$myFile}<br />\n"; } ?> cool thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267858 Share on other sites More sharing options...
Wildbug Posted June 4, 2007 Share Posted June 4, 2007 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++; } } Quote Link to comment https://forums.phpfreaks.com/topic/54169-solved-generate-random-file/#findComment-267863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.