Forum Moderators: coopster

Message Too Old, No Replies

Loop within a Loop?

         

RogueDogg

6:29 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



What I am trying to do here is with form I set the start folder number to be created let's say at 10 and the end to 100 so it will create folders from 10 to 100 incremented by 1. Now I have that working, and I have a copy script working, so now I what I need it to do is create the folder, copy the file into that folder and then run through the script again till it reaches the end folder number I specified. Hope that makes sense. Here is the code I have.


<?php
if( isset($_POST['submit']))
{
$start = ($_POST["start"]);
$end = ($_POST["end"]);
for($i=$start; $i<=$end; $i++){
mkdir("/path/to/create/folder/" . $i , 0755);
$file = "/path/to/file/example.txt";
$newfile = "/path/to/file/$i/example.txt.bak";

if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
}
}
?>

Now what that is doing is creating the folders I specify in the form, let's say 1 to 5 but it is copying the file(s) into the first folder 5 times. So folders 2 - 5 are empty but folder 1 has 5 text files in it. Any ideas how to fix this? I was told a loop within a loop but I'm lost.

Thanks in advance.

RogueDogg

8:05 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



Nevermind I have figured it out. Here is the line that needed to be modified:

$newfile = "/path/to/folder/and/". $i ."/file";

;-) cheers

jatar_k

8:17 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work :)