I have a script that searches through a directory for files containing a particular string (e.g 5126). It then renames each file that does have that string in its filename and prefixes the filename with another string (e.g. ‘coursefile5126). This bit works fine. After the string has been renamed, I want an email to be sent to the person whose file this is. At the moment, my script only emails the last person returned in the array, with the links to all files. Please see examples and code below.
example filename:
- Test_47-20120908-154525-5126.zip
- Test_48-20120908-155253-5126.zip
- Test_49-20120908-160226-5125.zip
example array:
- email1@email.com, 51, 26
- email2@email.com, 51, 25
As you see, test_47 and test_48 links should be sent to email1@email.com and test_49 should be sent to email2@email.com, but at the moment, all emails are sent to the last email in the array (email2@email.com in this example)
Can anyone please give me a clue as to where I am going wrong with this?
Thanks.
$dh = scandir("courses/");
...some SQL query here that returns an array
$i=0;
while ($data= mysql_fetch_array($query)) {
$j=1;
//this is the array returned by the SQL query
echo $data["email"]."-".$data["id_cart"]."-".$data["id_product"]."<br/>";
while ($j<sizeof($dh)) { //Ensures there are courses to look for
// this looks for courses that have not yet been renamed and prefixes them with the string 'coursefile' if necessary
if(end(explode("-",$dh[$j]))==$data["id_product"].$data["id_cart"].".zip" && reset(explode("-",$dh[$j])) != 'coursefile')
{
rename('courses/'.$dh[$j],'courses/'.'coursefile-'.$dh[$j]);
//sends email ---- this is where my problem lies I think
$to = $data["email"];
$subject = 'Your link to download your course';
$message = 'Link: http://www.website.com/courses/'.'coursefile-'.$dh[$j];
$headers = 'From: contact@website.com' . "\r\n" .
'Reply-To: contact@website.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
$j++;
}
}
Your code is a bit difficult to understand, but from your description here is my suggestion:
You can use glob to simplify your search and remove all the unnecessary conditionals. So the code could become:
HTH