【问题标题】:PHP: return only one time function in for loopPHP:在for循环中只返回一个时间函数
【发布时间】:2013-02-17 12:19:27
【问题描述】:

我有一个for 循环,在里面我上传了多张图片。上传图片后,我会通过wp_mail() 向管理员发送电子邮件通知。

一切正常并发送电子邮件通知。但是问题是通知电子邮件根据图像计数发送。表示如果 4 比 4 通知电子邮件(因为它在循环中),则上传 1 个图像而不是一封通知电子邮件。但是我希望通知只发送一次,无论是一张还是 10 张图片。

这是我的全部代码:

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

    }

}

【问题讨论】:

  • 在循环之后发送通知一次,而不是在每次循环迭代期间。
  • 但是如何检查表单是否已提交?但是我试图在循环之后放置,但它仍在发送上传图像的次数。如果你不介意的话,你可以给更多的反对票 :) 就我学到新东西而言 :)
  • 附加提示:如果您的操作成功完成,请不要渲染 HTML - 重定向到自己。如果/当用户在浏览器中使用“后退”按钮时,这有助于防止重新提交表单时出现问题。您可以将有关成功/失败的信息添加到会话中,并在重定向后将其读回。
  • 是的,所以您可以使用header( 'Location: /your/upload/form.php?success=' . ($isSuccess ? 1 : 0) ); exit();。然后在您的页面中,检测success 查询字符串并报告上传成功,可能使用$_SESSION(或它的WP 包装器)来存储您希望报告的每次上传数据(例如文件名)。
  • 好的!明白了..非常感谢您的帮助..感谢。

标签: php wordpress email for-loop upload


【解决方案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);
     }

    }

【讨论】:

  • $msg = false;$msg.='...' PHP 是否将布尔值转换为字符串而没有警告?
  • 没有发送电子邮件通知的功能完全没有问题,但只有在 for 循环中的事情是发送图像上传的次数。表示如果用户选择 3 张图片上传而不是电子邮件也发送 3 次
  • 除了 5.3 之外,不能代表所有人,是的
  • @pixelngrain 这只会发送一封电子邮件,但我会使用$msg=''; 而不是$msg = false;if (!empty($msg)) 而不是if ($msg) 以避免混合类型。
  • @popnoodles - 是的,你的更好,但我一直很喜欢 PHP,因为它能够如此轻松地处理差异类型。如果他们改变我被搞砸了:/
【解决方案2】:

简单的工作:

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

【讨论】:

    【解决方案3】:

    将要通知的帖子 ID 收集到一个数组中,然后在 for 循环调用 email_notification 之后将 ID 数组传递给它。修改 email_notification 函数以接受 ID 数组而不是一个 ID,并在电子邮件中包含所有 ID(例如,使用 foreach 循环遍历 ID 数组)。

    【讨论】:

    • 很抱歉造成混乱,但这仅适用于一篇文章。图片上传表单在单个帖子上
    【解决方案4】:

    在循环内使用布尔变量。最初将其设置为 true,然后 && 与单个图像上传成功。

    然后,在循环完成执行后,您可以检查该变量并发送您的电子邮件通知。

    即。

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

    如果您仍需要电子邮件中的 id,则需要在循环期间收集它们。

    【讨论】:

    • 哦,没用过这个。需要帮助以了解放置位置和使用方法
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2017-04-03
    相关资源
    最近更新 更多