multiple image uploader on a wordpress meta box

I’ trying to create 8 fields to upload images on a post, using add_meta_box, but for some reason some images (jpeg) will not be uploaded, regardless of their file size been correct and the width and height to be also correct.

The fields:

Read More
    function facilitiesimage_box_render(){
    $status_message = get_post_meta($post->ID, 'facilities_img_error', true);
            if($status_message) {
                echo '<div class="error" id="message"><p>' . $status_message . '</p></div>';
            }

            $max_no_img=8; // Maximum number of images value to be set here


            echo '<input type="hidden" name="MAX_FILE_SIZE" value="1524288">';
            echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
            for($i=1; $i<=$max_no_img; $i++){
                echo "<tr><td>Images $i</td><td><input type=file name='images[]' class='bginput'></td></tr>";
            }
            echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
            echo "</form> </table>";
}

The rest of the code:

function update_facilitiesimage($post_id, $post) {


        while(list($key,$value) = each($_FILES['images']['name'])) {
            if(!empty($value)){
                $arr_file_type = wp_check_filetype(basename($value)); 
                $uploaded_file_type = $arr_file_type['type'];
                $allowed_file_types = array('image/jpg', 'image/jpeg', 'image/gif', 'image/png');
                if(in_array($uploaded_file_type, $allowed_file_types)){
                    $upload_dir = wp_upload_dir();
                    $newname = strtolower(str_replace(' ', '-', $value));
                    $add = $upload_dir['path'] . '/' . $newname;
                    copy($_FILES['images']['tmp_name'][$key], $add);
                    list($width, $height) = getimagesize($add);
                    $upload_image_width = '630';
                    $upload_image_height = '350';
                    if($width < $upload_image_width || $height < $upload_image_height) {
                        $upload_feedback = 'Sorry, the image ' . $value . ' does not meet the minimum height/width requirements. Please upload another image.<br/>';
                        update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
                        unlink($add);
                    } 
                    else {
                        $upload_image_width = '630';
                        $upload_image_height = '350';
                        $resized = image_resize($add, $upload_image_width, $upload_image_height, true);
                        unlink($add);
                        $file_name_and_location = $add;
                        $file_title_for_media_library = 'your title here';
                        $attachment = array(
                            'post_mime_type' => $uploaded_file_type,
                            'post_title' => 'Uploaded image ' . addslashes($file_title_for_media_library),
                            'post_content' => '',
                            'post_status' => 'inherit'
                        );
                        $attach_id = wp_insert_attachment( $attachment, $file_name_and_location );
                        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                        $attach_data = wp_generate_attachment_metadata( $attach_id, $file_name_and_location );
                        wp_update_attachment_metadata($attach_id,  $attach_data);
                        $ra = 'attached_facility-img' . $key;
                        $existing_uploaded_image = (int) get_post_meta($post_id, $ra, true);
                        if(is_numeric($existing_uploaded_image)) {
                            wp_delete_attachment($existing_uploaded_image);
                        }
                        update_post_meta($post_id, $ra ,$attach_id);
                        $upload_feedback = false;
                        update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
                        $fname = strrchr($resized, '/');
                        $img = $upload_dir['baseurl'] . $fname;
                        $table = 'facilitiesimage-' . $key;
                        delete_post_meta($post_id, $table);
                        update_post_meta($post_id, $table, $img);
                    }
                }
                else {
                    $upload_feedback = 'Please upload only image files (jpg, gif or png).<br/>';
                    update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
                }
            }
        }
    }

add_action('save_post','update_facilitiesimage',1,2);

Before hand I’m just learning so I apologize for any HUGE ERROR (or horror ) in my code.

All that was working fine until I got some images that will not update, these images are jpeg, and the file size and dimensions are whiting the values in the code.

Using:

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";

I found that the values are correct (the image pass the “validation”) but the image don’t get uploaded. Note that the script don’t have problems to upload other jpeg images.

Any Help will be appreciated

Thanks in advance

Related posts

Leave a Reply

1 comment

  1. The fields are ok here is the update_facilitiesimage function for you with some improvements

    function update_facilitiesimage() {
        $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
        global $post;
        require_once(ABSPATH . '/wp-admin/includes/image.php');
        while(list($key,$value) = each($_FILES['images']['name'])) {
          if(!empty($value)){
    
            $gymname = strtolower(str_replace(' ', '-', get_the_author_meta( 'gymname' , $post->post_author )));
            $newname = strtolower(str_replace(' ', '-', $value));
            $upload_dir = wp_upload_dir();
            $add = $upload_dir['path'] . '/' . $gymname . '-' . $newname;
            copy($_FILES['images']['tmp_name'][$key], $add);
            $arr_file_type = wp_check_filetype(basename($add)); 
            $uploaded_file_type = $arr_file_type['type'];
            $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
            if(in_array($uploaded_file_type, $allowed_file_types)) { 
              list($width, $height) = getimagesize($add);
              $upload_image_slideshow_width = '630';
              $upload_image_slideshow_height = '350';
              if(($width > $upload_image_slideshow_width) || ($height > $upload_image_slideshow_height)) {
                $resized = image_resize($add, $upload_image_slideshow_width, $upload_image_slideshow_height, true);
                unlink($add);
                $add = $resized;
                $fname = strrchr($add, '/');
                $img = str_replace('/', '', $fname);
                $table = 'facilitiesimage-' . $key;
                if(!get_post_meta($post_id, $table)){
                  add_post_meta($post_id, $table, $img);
                } 
                else {
                  $oldfile = get_post_meta($post_id, $table, true); 
                  $oldfilelocation = $upload_dir['path'] . '/' . $oldfile;
                  unlink($oldfilelocation);            
                  update_post_meta($post_id, $table, $img);
                }
                delete_post_meta($post_id, 'facilities_img_error');            
              }
              elseif (($width < $upload_image_slideshow_width) || ($height < $upload_image_slideshow_height)) {
                $upload_feedback = 'Sorry, but the image ' . $value . ' does not meet the minimum height/width requirements. Please upload another image.';
                if(!get_post_meta($post_id, 'facilities_img_error')){
                  add_post_meta($post_id, 'facilities_img_error', $upload_feedback);
                } 
                else {
                  update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
                }
                unlink($add);
              }
              elseif (($width == $upload_image_slideshow_width) && ($height == $upload_image_slideshow_height)) {
                $fname = strrchr($add, '/');
                $img = str_replace('/', '', $fname);
                $table = 'facilitiesimage-' . $key;
                if(!get_post_meta($post_id, $table)){
                  add_post_meta($post_id, $table, $img);
                } 
                else {
                  $oldfile = get_post_meta($post_id, $table, true); 
                  $oldfilelocation = $upload_dir['path'] . '/' . $oldfile;
                  unlink($oldfilelocation);            
                  update_post_meta($post_id, $table, $img);
                }
                delete_post_meta($post_id, 'facilities_img_error');
              }          
            }
            else {
              $upload_feedback = 'Please upload only image files (jpg, gif or png).';
              if(!get_post_meta($post_id, 'facilities_img_error')){
                add_post_meta($post_id, 'facilities_img_error', $upload_feedback);
              } 
              else {
                update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
              }
            }
          }
        }
      }
    add_action('save_post','update_facilitiesimage');