Wordpress: uploading images using php - can't create different image sizes -
i trying simulate wordpress default upload process. here go images:
<input type="file" name="imgsupload1[]" id="imgsupload1[]" multiple="">
the code should upload:
$post_imgs = $_files['imgsupload1']; foreach ($post_imgs['name'] $key => $value) { if ($post_imgs['name'][$key]) { $file = array( 'name' => $post_imgs['name'][$key], 'type' => $post_imgs['type'][$key], 'tmp_name' => $post_imgs['tmp_name'][$key], 'error' => $post_imgs['error'][$key], 'size' => $post_imgs['size'][$key] ); $img_caption = $img_captions[urlencode(basename($file['name']))]; $new_file = wp_handle_upload($file, array( 'test_form' => false )); $filename = $new_file['url']; $filetype = wp_check_filetype( basename( $filename ), null ); $wp_upload_dir = wp_upload_dir(); // prepare array of post data attachment. $attachment = array( 'guid' => $wp_upload_dir['url'].'/'.basename( $filename ), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 'post_content' => '', 'post_status' => 'inherit', 'post_excerpt' => $img_caption ); require_once( abspath.'wp-admin/includes/image.php' ); $img_id = wp_insert_attachment( $attachment, $filename, $post_id ); $img_data = wp_generate_attachment_metadata( $img_id, $wp_upload_dir['basedir'].'/'.basename( $filename ) ); wp_update_attachment_metadata( $img_id, $img_data ); } // if image[key] exists } // foreach uploaded image
the image size defined default not created. seems problem in wp_generate_attachment_metadata
the problem in wp_generate_attachment_metadata, passing proper absolute path, need use 'path' element in wp_upload_dir array:
$img_data = wp_generate_attachment_metadata( $img_id, $wp_upload_dir['path'].'/'.basename( $filename ) );
which different written in codex. hope someone.
Comments
Post a Comment