I've set up a custom post type and would like to have the ability to attach files to those posts. I've got the CPT and metabox set up, and the upload function as well.

When I display the CPT on the front end, I'd like there to be a download link for the file. To simplify things, I'd like to use the included MIME type icons (wp-includes/images/crystal/document.png for example). Using wp_upload_bits() there's no MIME in the meta array.

Is it possible to add the MIME type to the function? Or should I be looking at another function/method of identifying the MIME?

Here's my upload code:

 $upload = wp_upload_bits($_FILES['resources_attachment']['name'], null, file_get_contents($_FILES['resources_attachment']['tmp_name']));

 if(isset($upload['error']) && $upload['error'] != 0) {
   wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
 } else {
 add_post_meta($id, 'resources_attachment', $upload);
 update_post_meta($id, 'resources_attachment', $upload);       

1 Answer 1


Ended up using wp_handle_upload(), which puts MIME type into the DB automatically. See http://codex.wordpress.org/Function_Reference/wp_handle_upload

One thing to note, this function enters the file in the DB as post meta and not as an attachment. wp_insert_attachment() would do the latter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.