0

when out of ideas, thou shall turn to stackoverflow. so that's what i'm doing :) i need to fill up a directory with 90,000 random files (no content needed) but for some reason, my infinite loop script is only creating about 10 files (from 1-10) and nothing more. what am i doing wrong?

 <?
 $counter = 1;
 while ($counter < 90000) {
 $rand = rand(1, 9999999999999999999999);
 print (" counter = " . $counter . "<BR>");
 $ourFileName = "$rand";
 $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
 fclose($ourFileHandle);
 $counter++;
 }
 ?> 
5
  • 3
    Consider the limits of integer values in 32-bit PHP... then why on earth do you need 90,000 (way too many to be sensible for a directory) randomly named empty files.
    – Mark Baker
    Apr 28, 2012 at 15:07
  • Why not use for loop? for ( $counter = 1 ; $counter < 90000 ; $counter++). Won't fix anything, but least its more readable. Apr 28, 2012 at 15:15
  • Can anyone give me just one practical use for this code?
    – itachi
    Apr 28, 2012 at 15:24
  • why downvote this? you may think the code or purpose is bad, but that doesn't make a bad question. +1 to compensate.
    – goat
    Apr 28, 2012 at 15:26
  • The reason isn't malicious or bad, but I need to test another script and if it can handle 90,000 files or not. Instead of creating 90,000 files manually I thought it's easier to do it with a PHP script. Any better ideas to fill a directory with 90,000 files? Apr 28, 2012 at 17:16

2 Answers 2

2

Here's why:

php > $x = 9999999999999999999999;
php > echo $x;
1.0E+22
php > echo rand(1, 9999999999999999999999);
1
php > echo rand(1, 9999999999999999999999);
1
php > echo rand(1, 9999999999999999999999);
1
php > echo rand(1, $x);
1
0
$NoOfFiles = 10;

for($i = 0; $i < $NoOfFiles; $i++){
    $FileHandler = fopen($i.".txt", 'w') or die("can't open file");
    fclose($FileHandler);
    echo "File '". $i.".txt' has been created<br/>";
}

Just alter the $NoOfFiles variable to however many files you want creating...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.