Custom metabox for file upload return empty filename

1.4k Views Asked by At

I want to attach a file to a post and "do something with it later". The file isn't being pulled over when I publish/update my post, the error I get is an empty filename. If I change the input type to text and submit I can get the text to save/display but trying to upload the file acts as though I haven't supplied the file to the form:

form --

function display_file_upload_meta_box($post_id,$post){

wp_nonce_field( basename( __FILE__ ), 'file_upload_meta_box_nonce' );
    ?>
        <p>
            <?php
                $fileUpload= get_post_meta($object->ID,'file-upload-meta',true);
                if(!$fileUpload)
                    $fileUpload = '';
                echo 'file: '.$fileUpload;
            ?>
            <label for="file_upload_meta">Attach a file to this post</label>
            <input type="file" id="file_upload_meta" name="file_upload_meta" class="widefat"/>
        </p>
    <?php
    }

code to upload file --

$new_meta_value = wp_upload_bits($_FILES["file_upload_meta"]['name'], null, file_get_contents($_FILES["file_upload_meta"]['tmp_name']));
2

There are 2 best solutions below

0
On

I got a requirement for adding custom meta boxes for a custom post type in wordpress and I tried using the following plugin and got working for me. Try this

0
On

It's probably because the form does not have those attributes :

enctype="multipart/form-data" encoding="multipart/form-data"

You can use a hook to add them, check this : https://gist.github.com/rfmeier/3513349