2012-09-09 20 views
-1

我有一個腳本,通過目錄搜索包含特定字符串(例如5126)的文件。然後它重新命名每個文件,該文件的文件名中包含該字符串,並以另一個字符串作爲文件名的前綴(例如'coursefile5126)。這一點工作正常。字符串重命名後,我想要發送一封電子郵件給這個人的文件。目前,我的腳本只發送數組中返回的最後一個人,並鏈接到所有文件。請參閱下面的示例和代碼。我應該使用foreach來使這個PHP腳本按預期工作嗎?

例如文件名:

  • Test_47-20120908-154525-5126.zip
  • Test_48-20120908-155253-5126.zip
  • Test_49-20120908-160226-5125.zip

示例陣列:

正如你看到的,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++; 
     }    
     } 
+4

很抱歉,但你的代碼基本上是不可理解的。 –

+0

不幸的是我繼承了這個代碼,我以爲我會是唯一一個發現很難理解的人,顯然我不是 – Fred

回答

0

你的代碼是有點難以理解,但是從你的描述,這裏是我的建議:

您可以使用glob來簡化搜索並刪除所有不必要的條件語句。因此,代碼將變成:

// This needs to be adjusted to the current settings. 
$full_path = $current_path . '/courses'; 

...some SQL query here that returns an array 
while ($data = mysql_fetch_array($query)) 
{ 
    // this is the array returned by the SQL query 
    echo $data["email"]."-".$data["id_cart"]."-".$data["id_product"]."<br/>"; 

    // This is what we are looking for for every user 
    $key = $data["id_cart"] . $data["id_product"]; 

    foreach (glob("{$full_path}/*-{$key}.zip") as $filename) 
    { 
     // glob returns the full path so we need to split it from 
     // the actual filename. I am assuming that the $full_path 
     // is known and it is the full path for the courses/ folder. 
     // $real_filename contains the name of the file processed 
     // (used in checking if the file has been renamed and in renaming) 
     $real_filename = str_replace($full_path, '', $filename); 

     // Check if the file has already been processed i.e. renamed 
     if (substr($real_filename, 0, 11) != 'coursefile-') 
     { 
      // There are files so, rename and then email 
      rename($filename, $full_path . 'coursefile-' . $real_filename);       

      // sends email 
      $to  = $data["email"]; 
      $subject = 'Your link to download your course'; 
      $message = 'Link: http://www.website.com/courses/' 
        . 'coursefile-' . $real_filename; 
      $headers = 'From: [email protected]' . "\r\n" 
        . 'Reply-To: [email protected]' . "\r\n"; 

      $mail_status = mail($to, $subject, $message, $headers); 
     } 
    } 
} 

HTH

+0

謝謝尼古拉斯,它很有用。我不得不改變glob()模式,因爲它不會使用花括號(即使帶有標誌)。感謝您花時間想出更好的代碼。 – Fred

相關問題