我有一個腳本,通過目錄搜索包含特定字符串(例如5126)的文件。然後它重新命名每個文件,該文件的文件名中包含該字符串,並以另一個字符串作爲文件名的前綴(例如'coursefile5126)。這一點工作正常。字符串重命名後,我想要發送一封電子郵件給這個人的文件。目前,我的腳本只發送數組中返回的最後一個人,並鏈接到所有文件。請參閱下面的示例和代碼。我應該使用foreach來使這個PHP腳本按預期工作嗎?
例如文件名:
- Test_47-20120908-154525-5126.zip
- Test_48-20120908-155253-5126.zip
- Test_49-20120908-160226-5125.zip
示例陣列:
- [email protected],51,26
- [email protected],51,25
正如你看到的,test_47和test_48鏈接應該發送到[email protected]和test_49應送[email protected],但在所有的電子郵件都發送到陣列中的最後一封電子郵件(本例中爲[email protected])
任何人都可以請給我一個線索,告訴我哪裏出錯了嗎?
謝謝。
$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: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n";
mail($to, $subject, $message, $headers);
}
$j++;
}
}
很抱歉,但你的代碼基本上是不可理解的。 –
不幸的是我繼承了這個代碼,我以爲我會是唯一一個發現很難理解的人,顯然我不是 – Fred