title: wp_generate_attachment_metadata returning an empty array
body: <p>I'm writing a Wordpress plugin to download remote images on my blog.</p>
  
  <p>I made a function to upload a remote image locally, then returning its ID.
  All seems OK except that </p>
  
  <pre><code>    $attach_data = wp_generate_attachment_metadata( $attach_id, $local_file );
  </code></pre>
  
  <p>returns me an empty array - and it should not.</p>
  
  <p><a href="http://codex.wordpress.org/Function_Reference/wp_generate_attachment_metadata" rel="nofollow">wp_generate_attachment_metadata</a> is, among others, responsible of generating the thumbnails of the uploaded image.  But I have no thumbnails created when I run my code.</p>
  
  <p>I checked the values I send to the function and they seems correct : I have an ID and an absolute path to the uploaded file, as documented in the codex.
  Still, I can't manage to have my code working :</p>
  
  <p><strong>$attach_data</strong> should not be empty...</p>
  
  <p>Can anyone help ?</p>
  
  <pre><code>function upload_from_remote_url($url,$post_id){
  
      $url = $this-&gt;validate_remote_media_url($url); //check file is not on local server
      if (!$url) return false;
  
      if ($existing_id = $this-&gt;media_already_exists($url)) return $existing_id; //url already has been downloaded
  
      $upload_dir = wp_upload_dir();
      $wp_mime_types = wp_get_mime_types();
  
      //fetch image
      $response = wp_remote_get( $url );
  
      //get filename without extension
      $filename = basename( $url ); //get filename &amp; extension
      $filename_strip = preg_replace('/\.[^.]*$/', '', $filename); //strip extension
  
      //get extension from content type,
      //because wp_upload_bits needs an extension and certain url don't have one.
  
      $file_type = wp_remote_retrieve_header( $response, 'content-type' );
      $extensions = array_search($file_type,$wp_mime_types);
      $extensions_arr = explode('|',$extensions);
      $extension = $extensions_arr[0];
  
      $new_filename = $filename_strip.'.'.$extension; //full name
      $new_filename = wp_unique_filename($upload_dir['path'], $new_filename); // be sure this name do not exist already
  
      $uploaded = wp_upload_bits($new_filename, '', wp_remote_retrieve_body( $response ) );
      if ($uploaded['error']) return false;
  
      $local_file = $uploaded['file'];
      $local_filename = basename($local_file);
      $local_filetype = wp_check_filetype( $local_filename, null );
  
      //Attachment options
      $attachment = array(
          'post_title'=&gt; $local_filename,
          'post_mime_type' =&gt; $local_filetype,
          'post_status' =&gt; 'inherit'
      );
  
      // Add the image to your media library
      $attach_id = wp_insert_attachment( $attachment, $local_file, $post_id );
  
      if (!$attach_id) return false;
  
      $attach_data = wp_generate_attachment_metadata( $attach_id, $local_file );
  
      wp_update_attachment_metadata( $attach_id, $attach_data );
  
      //save source link so we do not import several times the same media
      update_post_meta($attach_id, 'grm_source', $url);
  
      return $attach_id;
  
  }
  </code></pre>
  
  <p>BTW, if any WP gourou had anything to say about this code... I'll be happy to read it, as the WP documentation about uploading files is a bit messy.  I needed some specific stuff here, as being able to retrieve the file extension.  I ended up to this but maybe you have some better ideas !</p>