Jump to content

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


redarrow

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]
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites


This was for learning the file statements for fun.

i used

fwrite
unlink
array
file_exists
foreach
unlink

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){

unlink("$dirname/$del/test.txt");
}

$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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.