I'm trying to upload a PDF file for a custom post type called Products using wp_upload_bits. I'm only doing this in my wp-admin. I don't want these files added to the media library, I just want to upload them and return the URL for when I display the Product.
However when I do I get the error that it is an invalid file type returned from the function. I've set the enctype using this in my functions.php
//Allow file uploads
function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
Here is my code for generating the metaboxes on the admin menu
function products_pdf_uploads_show_meta() {
global $meta_box_pdf_uploads, $post, $prefix;
echo '<table class="form-table">';
echo '<p class="description">Upload your PDFs here</p>';
foreach ($meta_box_pdf_uploads['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<td>',
//'<input type="file" style="width: 700px;" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : null, '" />'
'<input type="file" style="width: 700px;" name="', $field['id'], '" id="', $field['id'], '" />',
'</td>',
'</tr>';
}
echo '</table>';
}
add_meta_box($meta_box_pdf_uploads['id'], $meta_box_pdf_uploads['title'], 'products_pdf_uploads_show_meta', $meta_box_pdf_uploads['page'], $meta_box_pdf_uploads['context'], $meta_box_pdf_uploads['priority']);
It doesn't have anything to do with the last line does it? Where I'm calling add_meta_box and passing the custom post type of PRODUCT and not specifiying PAGE or POST? Just a though.
Here's my code for uploading the metabox PDF file:
//Upload PDF files
foreach ($meta_box_pdf_uploads['fields'] as $pdf_field) {
//put file array into a variable
$pdf = $_FILES[$pdf_field['name']];
//if array is set and there is no error
if(isset($pdf['error']) && $pdf['error'] > 0) {
//setup error handling based on error code
wp_die('Error uploading file: Error Number is ' . $pdf['error']);
} else { //Passed
//setup file type allowed
$supported_file_type = array('application/pdf');
//Get the file type
$uploaded_file_type = $pdf['type'];
//check if file type is allowed
if(in_array($uploaded_file_type, $supported_file_type)) {
//upload file
$uploaded_file = wp_upload_bits($_FILES[$pdf_field['name']], null, file_get_contents($_FILES[$pdf_field['tmp_name']]));
print_r($uploaded_file);
wp_die('did it upload?');
add_post_meta($post_id, $pdf_field['id'], $uploaded_file);
update_post_meta($post_id, $pdf_field['id'], $uploaded_file);
} else { // NOT A PDF
wp_die('The file you tried to upload was not a PDF');
}
}
When I do a print_r($uploaded_file) to see what the function returns I get Array ( [error] => Invalid file type )
I've checked my wp-includes/functions.php and it apparently allows the file types under the get_allowed_mime_types() function.
'pdf' => 'application/pdf',
Am I missing something?
Here is the output from var_dump($_FILES). I currently have two fields, when I'm testing I'm only selecting a file to upload for the first one.
array(2) { ["aps_pdf1"]=> array(5) { ["name"]=> string(12) "1testpdf.pdf" ["type"]=> string(15) "application/pdf" ["tmp_name"]=> string(14) "/tmp/phpwdXK0T" ["error"]=> int(0) ["size"]=> int(8278) }
["aps_pdf2"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } }
var_dump($_FILES)please.