Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:09:22+00:00 2026-06-11T03:09:22+00:00

I have a script that searches through a directory for files containing a particular

  • 0

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++;
       }             
      }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:09:23+00:00Added an answer on June 11, 2026 at 3:09 am

    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:

    // 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that searches for a directory containing a specific file, starting
I have following script that executes all the .reg files in the current directory.
If I have a script that writes 1000 lines to file and then continues
Currently im faced with the following problem: I have a script that searches through
I have a jquery script that searches a php file that produces an XML
I have a script that searches through urls and looks for a certain domain
I have a line in a bash script that searches for a folder using
At the minute I have a page with an AJAX script that searches a
I have script that reads remote file content and writes it to local server.
I have a script that recursively loops through all the sub directories and compresses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.