2015-06-14 161 views
0

我想模擬WordPress的默認上傳過程。 何去何從圖像:WordPress的:使用PHP上傳圖像 - 無法創建不同的圖像大小

<input type="file" name="imgsupload1[]" id="imgsupload1[]" multiple=""> 

的代碼應該上傳:未創建

$post_imgs = $_FILES['imgsupload1'];  
foreach ($post_imgs['name'] as $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 an array of post data for the 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 

默認定義圖像尺寸。 看來問題是wp_generate_attachment_metadata

回答

0

的問題是在wp_generate_attachment_metadata,用於傳遞正確的絕對路徑,你需要使用wp_upload_dir陣「路徑」元素:

$img_data = wp_generate_attachment_metadata($img_id, $wp_upload_dir['path'].'/'.basename($filename)); 

這是從什麼不同以Codex編寫。希望它能幫助別人。