-1

我有一个脚本,它在目录中搜索包含特定字符串(例如 5126)的文件。然后,它重命名文件名中包含该字符串的每个文件,并在文件名前加上另一个字符串(例如'coursefile5126)。这一点工作正常。重命名字符串后,我希望将电子邮件发送给文件所在的人。目前,我的脚本只向数组中返回的最后一个人发送电子邮件,其中包含所有文件的链接。请参阅下面的示例和代码。

示例文件名:

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

示例数组:

  • 电子邮件1@email.com, 51, 26
  • email2@email.com, 51, 25

如您所见,test_47 和 test_48 链接应该发送到 email1@email.com,test_49 应该发送到 email2@email.com,但目前,所有电子邮件都发送到数组中的最后一个电子邮件 (email2@email.com)。 com 在这个例子中)

谁能给我一个线索,让我知道我在哪里出错了?

谢谢。

 $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++;
       }             
      }
4

1 回答 1

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: contact@website.com' . "\r\n" 
                     . 'Reply-To: contact@website.com' . "\r\n";

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

高温高压

于 2012-09-09T05:15:54.467 回答