0

forループがあり、その中に複数の画像をアップロードしています。画像がアップロードされたら、を介して管理者にメール通知を送信しますwp_mail()

物事は正常に機能しており、電子メール通知も送信しています。ただし、問題は、画像数ごとに送信される通知メールです。通知メールが4つより4つある場合は、1つの通知メールよりも1つの画像をアップロードすることを意味します(ループ内にあるため)。ただし、1枚でも10枚でも通知を1回だけ送信したい。

これが私のコード全体です:

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;                    
            }                
        }

    }

}
4

4 に答える 4

1

電子メールの内容を1つの変数(以下の$ msg)に保存し、ループの反復ごとに情報を修正します。ループが終了したら、変数が変更されているかどうかを確認し、変更されている場合はその内容をメールで送信します。

これは機能するはずです:

    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);
     }

    }
于 2013-02-17T12:25:34.473 に答える
1

easy work :

$message = "" ;
for($x = 0; $x < count($files['name']); $x++) {
    if(empty($errors)) {  
       move_uploaded_file($file_tmp, $upload_dir.$file_name);
       $message .= "$file_name successfuly uploaded in ". Date("H:i:s") ." ".$post->ID." \n\r" ;
       # something else  ... 
    }
    # something else  ... 
}
if($message!='')
   self::email_notification($message);
于 2013-02-17T12:27:21.623 に答える
0

Collect post ID's to be notified into an array, and then after the for loop call email_notification passing the array of ID's to it. Modify the email_notification function to accept an array of IDs instead of one ID, and include all IDs in the email (iterate through the ID array with a foreach loop for example).

于 2013-02-17T12:25:42.167 に答える
-1

Use a boolean variable inside the loop. Set it to true initially, and && it with the success of the individual image uploads.

Then, after the loop finishes executing, you can check that variable and send your email notification.

ie.

if ({success}) {
  $boolVar = $boolVar && true;
} else {
  $boolVar = $boolVar && false;
}

If you still want the ids in the email, you'll need to collect them during the loop.

于 2013-02-17T12:27:16.697 に答える