PHP:在for循环中只返回一次函数

问题描述:

我有一个for循环,并且在其中我上传了多个图像。上传图片后,我会通过wp_mail()向管理员发送电子邮件通知。PHP:在for循环中只返回一次函数

事情工作正常,并发送电子邮件通知。然而问题是通知电子邮件发送按照图像计数。意思是如果上传1张图片比4张通知电子邮件中的一张通知邮件(因为它在循环中)。不过,我希望通知只发送一次,无论是一个或10个图像。

这里是我的全部代码:一个变量(下文$ MSG)和修改信息循环的每一次迭代

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

    } 

} 
+2

循环*后发送通知*一次,而不是在每个循环迭代。 – deceze 2013-02-17 12:22:47

+0

但比如何检查表格是否已提交?不过,我试图放置循环后,但它仍然发送上传时间图像的数量。如果你不能介意的话,你可以给予更多的否定票:)只要我学到新东西:) – 2013-02-17 12:24:29

+0

更具体地说,因为如果没有错误,你正在发电子邮件记录'$ success = true',你的脚本当前发送一封电子邮件并且在循环之外,询问“if($ success)”,如果是,则发送电子邮件。 – Popnoodles 2013-02-17 12:24:48

存储您的电子邮件内容。当循环结束时,检查变量是否已被修改,如果是这样的话通过电子邮件发送它的内容。

这应该工作:

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

'$ msg = false;'和'$ msg。='...''PHP是否将bool转换为没有警告的字符串? – Popnoodles 2013-02-17 12:26:35

+0

没有完全发送电子邮件通知的功能没有问题,但只有它在for循环中发送图像上传的次数。意思是如果用户选择3张图片上传,比上传电子邮件还要多发3次 – 2013-02-17 12:28:18

+0

不能说全部,但是5.3呢,yup – 2013-02-17 12:29:57

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

+0

我很抱歉混淆,但这只是一个职位。图片上传表单在单个帖子 – 2013-02-17 12:26:55

在循环中使用布尔变量。首先将其设置为true,然后将其与单个图像上传的成功进行比较。

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

即。

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

如果您仍然希望在电子邮件的ID,你需要在循环过程中收集它们。

+0

哦,从未使用过这个。需要帮助以了解在哪里放置以及如何使用 – 2013-02-17 12:29:37

容易的工作:

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

您的答案也可以。..非常感谢 – 2013-02-17 12:43:52