phpimagewordpressformsfile-upload

WordPress: trying to upload image, the media_handle_upload() won't work


Trying to make a form to upload an image to Wordpress, then set that image as a post's thumbnail. But I can't seem to get the media_handle_upload() function to work properly.

the form's input for the file

 <input type="file" name="image" />

then this is the server side code

media_handle_upload( $_FILES['image'], 22 );

and this is what I get returned

object(WP_Error)#212 (2) { ["errors"]=> array(1) { ["upload_error"]=> array(1) { [0]=> string(212) "File is empty...." } } ["error_data"]=> array(0) { } }

I have played around with a couple different ways to enter the file variable,but none seems to work, am I doing that from, what exactly is the $file_id;


Solution

  • Looking at documentation for media_handle_upload(), the first parameter should be the name of the index for the file in the $_FILES array, so in your case it should look like this:

    media_handle_upload('image', 22);