#### Archived

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

# [solved] fun with fwrite mkdir unlink foreach rmdir thanks all

## Recommended Posts

I am trying to get the rand number to come out in the for loop as 10 new round numbers can you help please cheers.

please give an example and exsplain the example cheers.

[code]
<?php

\$a=rand(1,5000);

for(\$i=0; \$i<9; \$i++){

echo "\$a<br>";
}

?>

[/code]

The result but not as mixed numbers.

[code]
4962
4962
4962
4962
4962
4962
4962
4962
4962
[/code]

this is the long way that the above should do

[code]
<?php

\$a=rand(1,5000);
\$b=rand(1,5000);
\$c=rand(1,5000);
\$d=rand(1,5000);
\$e=rand(1,5000);
\$f=rand(1,5000);
\$g=rand(1,5000);
\$h=rand(1,5000);
\$i=rand(1,5000);
\$j=rand(1,5000);

echo " \$a<br> \$b<br> \$c<br> \$d<br> \$e<br> \$f<br> \$g<br> \$h<br> \$i<br> \$j<br>";

?>
[/code]

result but should work as first code above with the result of 10 new numbers.
[code]
4036
2456
4840
2138
1618
4250
4429
177
4425
526
[/code]

##### Share on other sites
Why not:
[code]
<?php
//incase you want to know the random number for later
for(\$i=0; \$i<9; \$i++){
\$rand[\$i]=rand(1,5000);
echo \$rand[\$i]."<br>";
}

//incase you just want to echo random numbers
for(\$i=0; \$i<9; \$i++){
echo(rand(1,5000));
}

?>[/code]

Orio.

##### Share on other sites
thank you so much

why can this not make all the 10 directory cheers.

makes the first dir but not the other 10 in thedir
[code]
<?php
for(\$i=0; \$i<9; \$i++){
\$rand[\$i]=rand(1,5000);
\$dir=\$rand[\$i]."<br>";
\$a=mkdir(thedir);
mkdir("thedir/\$dir");
}
?>
[/code]

##### Share on other sites
Well the problem you are executing this line:
\$a=mkdir(thedir);
each time. Now when PHP goes through the loop the first time it creates the directory thedir, but the secound time it goes to create it again. But it cant as thedir already exists. So move [code=php:0]\$a=mkdir(thedir);[/code] out of the for loop and place it at the top.

Also remove [code=php:0]."<br>"[/code] from this [code=php:0]\$dir=\$rand[\$i]."<br>";[/code]

Now your code should now work. This is what you code should loook like:
[code=php:0]<?php

// we create the directory first
mkdir('thedir');

for(\$i = 0; \$i < 9; \$i++)
{
\$rand[\$i] = rand(1, 5000);

\$dir = \$rand[\$i];

mkdir("thedir/\$dir");

echo "Created thedir/" . \$dir . "<br />\n";
}

?>[/code]

##### Share on other sites
thank you wild teen learning getting there

know if i wanted to put the dir into a array i use blob then if i wanted to fwrite into the dir chose via array blob how cheers mate.

##### Share on other sites
Is this delete format wrong cheers.

[code]
<?php
\$dirname="thedir";

\$del=array("a","b","c");

foreach(\$del AS \$deleted){

}
<?
[/code]

##### Share on other sites

This was for learning the file statements for fun.

i used

fwrite
array
file_exists
foreach

all the code does is delete any file that was test.txt then deletes any dir a.b.c
then creates a dir a,b,c then writed to the dir then prints what its done good fun.

[code]
<?php

\$dirname="thedir";

\$del_files=array("a","b","c");

foreach(\$del_files AS \$del){

}

\$dirname="thedir";

\$del=array("a","b","c");

foreach(\$del AS \$deleted){

rmdir("\$dirname/\$deleted");
}
rmdir("thedir");

\$dirname="thedir";

\$a=mkdir(\$dirname);

\$d=array("a","b","c");

foreach(\$d AS \$a){

\$x=mkdir("\$dirname/\$a");

echo " dir made is \$a <br>";

}

\$mess="this is redarrow";

\$a=fopen("\$dirname/a/test.txt","a");

if(file_exists("\$dirname/a/test.txt")){

fwrite(\$a,\$mess);

echo " message writen test.txt<br>";
}

\$mess="redarrow loves php";

\$a=fopen("\$dirname/b/test.txt","a");

if(file_exists("\$dirname/b/test.txt")){

fwrite(\$a,\$mess);

echo " message writen test.txt<br>";
}

\$mess="love php";

\$a=fopen("\$dirname/c/test.txt","a");

if(file_exists("\$dirname/c/test.txt")){

fwrite(\$a,\$mess);

echo " message writen test.txt";
}

?>
[/code]