-1

特定の文字列(5126など)を含むファイルをディレクトリで検索するスクリプトがあります。次に、ファイル名にその文字列が含まれている各ファイルの名前を変更し、ファイル名の前に別の文字列を付けます(例:'coursefile5126)。このビットは正常に機能します。文字列の名前が変更された後、これがファイルである人に電子メールを送信したいと思います。現時点では、私のスクリプトは、配列に最後に返された人にのみ、すべてのファイルへのリンクを電子メールで送信します。以下の例とコードを参照してください。

ファイル名の例:

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

配列の例:

  • email1 @ email.com、51、26
  • email2 @ email.com、51、25

ご覧のとおり、test_47とtest_48のリンクはemail1@email.comに送信し、test_49はemail2@email.comに送信する必要がありますが、現時点では、すべての電子メールは配列の最後の電子メール(email2 @ email)に送信されます。この例では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);
        }
    }
}

HTH

于 2012-09-09T05:15:54.467 に答える