Title: Upload File Through PHP, Error or Empty attachment.
Last modified: August 19, 2016

---

# Upload File Through PHP, Error or Empty attachment.

 *  Resolved [Xeross](https://wordpress.org/support/users/x3r0ss/)
 * (@x3r0ss)
 * [17 years ago](https://wordpress.org/support/topic/upload-file-through-php-error-or-empty-attachment/)
 * Hey,
 * I’m working on a script that can post stuff from outside wordpress.
    However 
   the following ain’t working.
 *     ```
       $post = array(
       	"post_title" => $posttitle,
       	"post_content" => $content,
       	"post_status" => "draft",
       	"post_author" => 1,
       	"post_category" => array(9, 10),
       	"tags_input" => array($show, $title, 'TV Show'),
       	"post_type" => "post",
       );
   
       $post_id = wp_insert_post( $post );
   
       $image = file_get_contents($image);
       file_put_contents("/data/www/blogfreestuff/public_html/tools/hulu/image.jpg", $image);
       $image = "/data/www/blogfreestuff/public_html/tools/hulu/image.jpg";
       echo $image;
   
       $attachment = array(
       	"post_title" => $show . " - " . $title . "(Thumb)",
       	"post_content" => "",
       	"post_status" => "draft");
   
       $id = wp_insert_attachment($attachment , $image, $post_id );
       wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $image ) );
       ```
   
 * It sais wp_generate_attachment_metadata doesn’t exist, so I included the file
   that contained the function, the error is gone, but the attachment gets inserted
   without image.
 * So can anyone help me out ?
 * Thanks, Xeross
 * Edit: I forgot i’m using wpmu

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [Xeross](https://wordpress.org/support/users/x3r0ss/)
 * (@x3r0ss)
 * [17 years ago](https://wordpress.org/support/topic/upload-file-through-php-error-or-empty-attachment/#post-1081828)
 * Update
 * > `$post = array(
   >  “post_title” => $posttitle, “post_content” => $content, “post_status”
   > => “draft”, “post_author” => 1, “post_category” => array(9, 10), “tags_input”
   > => “$show, $title, TV Show”, “post_type” => “post”, );
   > $post_id = wp_insert_post( $post );
   >  $image = “/data/www/blogfreestuff/public_html/
   > tools/hulu/image.jpg”; file_put_contents($image, file_get_contents($imageurl));
   > if(!file_exists($image))
   >  exit(“No File”);
   > $attachment = array(
   >  “post_title” => $show . ” – ” . $title . “(Thumb)”, “
   > post_content” => “”, “post_status” => “draft”, “post_mime_type” => “image/jpeg”);
   > $id = wp_insert_attachment($attachment, $image, $post_id );
   > $metadata = wp_generate_attachment_metadata( $id, $image );
   > $result = wp_update_attachment_metadata( $id, $metadata );`
 *  Thread Starter [Xeross](https://wordpress.org/support/users/x3r0ss/)
 * (@x3r0ss)
 * [17 years ago](https://wordpress.org/support/topic/upload-file-through-php-error-or-empty-attachment/#post-1081856)
 * Solved :D:D:D
 *  [demestav](https://wordpress.org/support/users/demestav/)
 * (@demestav)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/upload-file-through-php-error-or-empty-attachment/#post-1082317)
 * It would be useful if you could post your fix! Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Upload File Through PHP, Error or Empty attachment.’ is closed to new
replies.

 * 3 replies
 * 2 participants
 * Last reply from: [demestav](https://wordpress.org/support/users/demestav/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/upload-file-through-php-error-or-empty-attachment/#post-1082317)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
