Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been struggling with this for almost 2 days with no avail. I have a custom post type of PRODUCTS that has a group of metaboxes for uploading PDF files in the wp-admin. I've created the metaboxes in the admin. Here's the code for my metaboxes:

//PDF upload Meta Boxes
$meta_box_pdf_uploads = array(
    'id' => 'products-pdf-meta-boxes',
    'title' => "PDFs",
    'page' => 'product', //attach to products custom post
    'context' => 'normal',
    'priority' => 'default',
    'fields' => array(
        array(
            'name' => $prefix . 'pdf1',
            'desc' => 'Product PDF',
            'id' => $prefix . 'pdf1',
            'type' => 'text'
        ),
        array(
            'name' => $prefix . 'pdf2',
            'desc' => 'Product PDF',
            'id' => $prefix . 'pdf2',
            'type' => 'text'
        )
    )
);

For now there are only two pdf's to upload. Here is the code to show my metaboxes in 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'], '" />',
                '</td>',
             '</tr>';
    }

    echo '</table>';

}

All of this works until I try to check if the file upload field has a value or not. Here is what I'm working with now as far as the code to save the meta (there's tons of debugging code so you're aware):

 //Upload PDF files
    foreach ($meta_box_pdf_uploads['fields'] as $pdf_field) {

        //Make sure field isn't emtpy
        if(isset($_FILES[$pdf_field['name']]) {
            //var_dump($_FILES);
            print_r ($_FILES[$pdf_field['name']]);  //debugging
            $pdf_type = $_FILES[$pdf_field['name']['tmp_name']];  //debugging, can't get this to return any value
            wp_die('Passed! '. $pdf_type); //debugging
            //Setup File type allowed

            //Get the file type


        } else {
            //var_dump($_FILES);
            print_r ($_FILES[$pdf_field['name']]);
            wp_die('Nothing was passed: ' . $pdf_field['name']);  //debugging
        }
    }

Now even if I don't add a file to the upload field, my first condition is still met here:

if(isset($_FILES[$pdf_field['name']]) {

I know it's because it's returning an array of the $pdf_field. I've also tried check if the error was 0 using $_FILES[$pdf_field['name']['error'] but I can't return any of the $_FILE array values.

When specify a field to upload and I do a print_r ($_FILES[$pdf_field['name']]) it returns:

Array ( [name] => 1testpdf.pdf [type] => application/pdf [tmp_name] => /tmp/phpF7HQnJ [error] => 0 [size] => 8278 ) 

Which is correct, but it also does this when I dont' specify a field, only all the arrays values are empty except for the ERROR which equal 4 (no file specified).

I have been pulling my hair out over this. What is the best way to do this? I think if it was a single metabox and i could call it like $FILE['name_of_file']['error'] it would work. Why is using a foreach messing this up?

Thank you!

share|improve this question
    
Ahh I think I need to store my $_FILES($pdf_field['name']) array into a variable and go from there... –  drpcken Dec 3 '12 at 2:25
add comment

1 Answer

up vote 0 down vote accepted

Ahh I think I was able to answer this on my own. I created a variable for the $_FILES($pdf_field['name']) array and was able to use those to get the array values. Just need to do some error handling and test out my uploads. If there's something wrong with handling it this way please let me know.

 //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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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