2013-02-17 2 views
0

나는 for 루프를 가지고 있으며 그 안에 여러 개의 이미지를 업로드하고 있습니다. 이미지가 업로드되면 관리자에게 wp_mail()을 통해 이메일 알림을 전송합니다.PHP : for 루프에서 한 번만 함수를 반환하십시오.

정상적으로 작동하고 전자 메일 알림도 전송됩니다. 그러나 문제는 이미지 개수에 따라 알림 이메일을 보내는 것입니다. 4 개의 알림 전자 메일 4 개가 루프에있는 경우 1 개의 알림 전자 메일보다 1 개의 이미지를 업로드하는 경우를 의미합니다. 그러나 하나 또는 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

+0

양식을 제출했는지 여부를 어떻게 확인할 수 있습니까? 그러나 후 루프를 넣으려고했지만 여전히 업로드 이미지의 번호를 보내고있다. 당신이 결코 마음 먹을 수 없다면 더 많은 부정적인 투표를 할 수 있습니다 :) 지금까지 제가 새로운 것을 배울 수 있습니다. –

+0

더 구체적으로, 스크립트가 현재 이메일을 보내는 곳에'$ success = true'라는 에러가 없다면 이메일을 보내고 있습니다. 그리고 루프 밖에서'if ($ success)'를 물어보십시오. 그렇다면 이메일을 보내십시오. – Popnoodles

답변

1

스토어 이메일 내용 :

여기 내 전체 코드입니다. 루프가 끝나면 변수가 수정되었는지 확인합니다 (이메일 내용을 수정 한 경우).

이 작동해야

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

+0

이메일 알림을 보내는 기능이 전혀 없지만 for 루프에있는 것만 이미지를 업로드하는 시간을 보내고 있습니다. 사용자가 업로드 할 이미지를 3 개 선택하는 경우 이메일도 3 회 전송 –

+0

은 5.3을 제외한 모든 것을 말할 수 없으므로 –

0

수집 후 ID의이 배열로 통보되어야하는 다음 후에 그것을 ID 년대 배열 대피선 호 email_notification 대한. email_notification 함수를 수정하여 하나의 ID 대신 배열의 배열을 허용하고 전자 메일에 모든 ID를 포함시킵니다 (예 : foreach 루프와 함께 ID 배열을 반복).

+0

혼란 스럽지만 이것은 하나의 게시물에 대해서만입니다. 이미지 업로드 양식은 단일 게시판에 있습니다. –

-1

루프 내부에 부울 변수를 사용하십시오. 처음에는 true로 설정하고, 개별 이미지 업로드가 성공하면 & &으로 설정하십시오.

그런 다음 루프가 실행을 마친 후 해당 변수를 확인하고 전자 메일 알림을 보낼 수 있습니다.

즉.

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

이메일에 ID를 계속 입력하고 싶다면 루프 중에 해당 ID를 수집해야합니다.

+0

오 절대 사용하지 않았습니다. 장소 및 사용 방법을 이해하는 데 도움이 필요합니다. –

1

쉬운 작업 :

$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

답변이 너무 효과적입니다. 감사합니다. –

관련 문제