Forums

[resolved] Upload File Through PHP, Error or Empty attachment. (4 posts)

  1. Xeross
    Member
    Posted 2 years ago #

    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

  2. Xeross
    Member
    Posted 2 years ago #

    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 );`

  3. Xeross
    Member
    Posted 2 years ago #

    Solved :D:D:D

  4. demestav
    Member
    Posted 1 year ago #

    It would be useful if you could post your fix! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.