无限循环创建90,000个随机文件..但它只创建10个?

时间:2012-04-28 15:05:18

标签: php file loops infinite

当出于想法时,你将转向stackoverflow。这就是我正在做的事情:)我需要用90,000个随机文件填满一个目录(不需要任何内容​​)但由于某种原因,我的无限循环脚本只创建了大约10个文件(从1-10),仅此而已。我做错了什么?

 <?
 $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++;
 }
 ?> 

2 个答案:

答案 0 :(得分:2)

这就是原因:

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

答案 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/>";
}

只需将$ NoOfFiles变量更改为您想要创建的文件数量......