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

  • SEARCH
  • Home
  • 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 9304701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:46:02+00:00 2026-06-18T23:46:02+00:00

I have a for loop and inside it I am uploading multiple images. Once

  • 0

I have a for loop and inside it I am uploading multiple images. Once an image is uploaded, I send an email notification to admin via wp_mail().

Things are working fine and sending email notification as well. However the issue is that notification email sending as per image count. Means if upload 1 image than one notification email if 4 than 4 notification email (as it’s in the loop). However I want notification send only once no matter either one or 10 images.

Here is my entire code:

global $wpdb, $post;
$upload_path = wp_upload_dir();            
$post_id = $post->ID;

$upload_dir = $upload_path['basedir'].'/review-media/'.$post_id.'/'.get_current_user_id().'/';
$thumbnail_dir = $upload_dir.'thumbnail/';
$upload_url = $upload_path['baseurl'].'/review-media/'.$post_id.'/'.get_current_user_id().'/thumbnail/';


self::create_path($thumbnail_dir);


if (isset($_FILES['photo']) === true) {

    $errors = array();            
    $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

    $files = $_FILES['photo'];

    for($x = 0; $x < count($files['name']); $x++) {

        $file_name = $files['name'][$x]; 
        $file_ext = strtolower(end(explode('.', $file_name)));
        $file_size = $files['size'][$x];
        $file_tmp = $files['tmp_name'][$x];

        if($file_tmp)
        list($img_width, $img_height) = getimagesize($file_tmp);

        $min_img_width = 1;
        $min_img_height = 1;
        $max_img_width = 2000;
        $max_img_height = 2000;


        if (!$file_tmp) {
            $errors[] = '<p>Please select the file</p>';    
        }

        if (($file_tmp) && (($img_width > $max_img_width) || ($img_width < $min_img_width)) && (($img_height > $max_img_height) || ($img_height < $min_img_height))) {
            $errors[] = '<p>Size of <strong>'.$file_name.'</strong> must be within '. $min_img_width .'px to '. $max_img_width .'px</p>';
        }

        if ( ($file_tmp) && (in_array($file_ext, $allowed_ext) === false) ) {                
            $errors[] = '<p>Extension <strong>'.$file_ext.'</strong> not allowed</p>';
            unlink($file_tmp);                                
        }

        if ($file_size > 2097152) {                
            $errors[] = '<p>File <strong>'.$file_name.'</strong> size must be under 2mb</p>';
            unlink($file_tmp);                
        }

        if(empty($errors)) {   

            move_uploaded_file($file_tmp, $upload_dir.$file_name);

            //crop and resize to images and thumbnails
            $target_file = $upload_dir.$file_name; // original file
            $resized_file = $upload_dir.$file_name; // max resized file
            $med_file = $upload_dir.'medium-'.$file_name; // medium resized fiel
            $thumbnail = $thumbnail_dir.$file_name; // thumbnail file
            $large_width = 1024; // upload resize max width
            $large_height = 1024; // upload resize max height
            $thumb_width = 150; // thumbnail width
            $thumb_height = 150; // thumbnail height
            $med_width = $thumb_width * 1.5; // medium resized image width
            $med_height = $thumb_height * 1.5; // medium resized image height

            // resize to maximum width and height
            self::resize_image($target_file, $resized_file, $large_width, $large_height, $file_ext);
            // resize with 1.5 multi of thumb width and height to generate thumbnail
            self::resize_image($target_file, $med_file, $med_width, $med_height, $file_ext);     
            // crop image uisng medium resized image               
            self::crop_thumbnai(file_exists($med_file) ? $med_file : $target_file, $thumbnail, $thumb_width, $thumb_height, $file_ext);

            // delete medium resized file after thumbnail creation.
            if (file_exists($med_file))
                unlink($med_file);

            self::insert_image($file_name);

            //self::get_uploaded_image();
            echo '<div class="upload-status-thumb">';
            echo '<img src="'.$upload_url.$file_name.'" alt="imge" />';
            echo '</div>';

            // send notification email to admin on image upload
            self::email_notification($post_id);                   

        } else {                
            foreach ($errors as $error) {                    
                echo $error;                    
            }                
        }

    }

}
  • 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-18T23:46:03+00:00Added an answer on June 18, 2026 at 11:46 pm

    Store your email content in one variable ($msg below) and amend the information every iteration of the loop. When the loop has finished, check if the variable has been modified, if so email its content.

    This should work:

        global $wpdb, $post;
        $upload_path = wp_upload_dir();            
        $post_id = $post->ID;
        $msg = false;
    
        $upload_dir = $upload_path['basedir'].'/review-media/'.$post_id.'/'.get_current_user_id().'/';
        $thumbnail_dir = $upload_dir.'thumbnail/';
        $upload_url = $upload_path['baseurl'].'/review-media/'.$post_id.'/'.get_current_user_id().'/thumbnail/';
    
    
        self::create_path($thumbnail_dir);
    
    
        if (isset($_FILES['photo']) === true) {
    
            $errors = array();            
            $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
    
            $files = $_FILES['photo'];
    
            for($x = 0; $x < count($files['name']); $x++) {
    
                $file_name = $files['name'][$x]; 
                $file_ext = strtolower(end(explode('.', $file_name)));
                $file_size = $files['size'][$x];
                $file_tmp = $files['tmp_name'][$x];
    
                if($file_tmp)
                list($img_width, $img_height) = getimagesize($file_tmp);
    
                $min_img_width = 1;
                $min_img_height = 1;
                $max_img_width = 2000;
                $max_img_height = 2000;
    
    
                if (!$file_tmp) {
                    $errors[] = '<p>Please select the file</p>';    
                }
    
                if (($file_tmp) && (($img_width > $max_img_width) || ($img_width < $min_img_width)) && (($img_height > $max_img_height) || ($img_height < $min_img_height))) {
                    $errors[] = '<p>Size of <strong>'.$file_name.'</strong> must be within '. $min_img_width .'px to '. $max_img_width .'px</p>';
                }
    
                if ( ($file_tmp) && (in_array($file_ext, $allowed_ext) === false) ) {                
                    $errors[] = '<p>Extension <strong>'.$file_ext.'</strong> not allowed</p>';
                    unlink($file_tmp);                                
                }
    
                if ($file_size > 2097152) {                
                    $errors[] = '<p>File <strong>'.$file_name.'</strong> size must be under 2mb</p>';
                    unlink($file_tmp);                
                }
    
                if(empty($errors)) {   
    
                    move_uploaded_file($file_tmp, $upload_dir.$file_name);
    
                    //crop and resize to images and thumbnails
                    $target_file = $upload_dir.$file_name; // original file
                    $resized_file = $upload_dir.$file_name; // max resized file
                    $med_file = $upload_dir.'medium-'.$file_name; // medium resized fiel
                    $thumbnail = $thumbnail_dir.$file_name; // thumbnail file
                    $large_width = 1024; // upload resize max width
                    $large_height = 1024; // upload resize max height
                    $thumb_width = 150; // thumbnail width
                    $thumb_height = 150; // thumbnail height
                    $med_width = $thumb_width * 1.5; // medium resized image width
                    $med_height = $thumb_height * 1.5; // medium resized image height
    
                    // resize to maximum width and height
                    self::resize_image($target_file, $resized_file, $large_width, $large_height, $file_ext);
                    // resize with 1.5 multi of thumb width and height to generate thumbnail
                    self::resize_image($target_file, $med_file, $med_width, $med_height, $file_ext);     
                    // crop image uisng medium resized image               
                    self::crop_thumbnai(file_exists($med_file) ? $med_file : $target_file, $thumbnail, $thumb_width, $thumb_height, $file_ext);
    
                    // delete medium resized file after thumbnail creation.
                    if (file_exists($med_file))
                        unlink($med_file);
    
                    self::insert_image($file_name);
    
                    //self::get_uploaded_image();
                    echo '<div class="upload-status-thumb">';
                    echo '<img src="'.$upload_url.$file_name.'" alt="imge" />';
                    echo '</div>';
    
                    // add to message var
                   $msg .= 'image added: '.$post_id."\n";                   
    
                } else {                
                    foreach ($errors as $error) {                    
                        echo $error;                    
                    }                
                }
    
            }
    
         if($msg){ // if msg is no longer false email it to admin
            self::email_notification($msg);
         }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following loop inside an function init(); which is executed onload, I
I have some problems with controlling a while loop inside an event structure. Say
Hi I have a quick question, which is the easiest way to loop inside
I have a for loop. Inside that loop I want to fill up an
I have this block of code inside a loop: var points = [new google.maps.LatLng(lat1,
I have the following code inside a loop: Range(N & i + 1).Formula =
I have created a loop, inside there is an if statment but it doesnt
I have a for loop, inside which I'm calling a method to show the
i want to have a foreach loop where the initial array is changed inside
Sometimes I have to check for some condition that doesn't change inside a loop,

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.