Trying to Implement .pdf File Upload in Admin for plugin

This is the first time in my plugin development that I have had a need to upload a file. UH-OH!!!

I create my plugins OOP and I have always used jQuery .serialize to post the data to my plugin.php file but I can’t do that and upload a file.

Read More

The admin form that will allow the admins of the site to upload .pdf files for their users:

<h3>
            <?php 
                echo '<img class="btnAddDoc" src="' . plugins_url('images/icn/Add16.png', dirname(__FILE__)) . '" >';
            ?>
            &nbsp;
            <span class="btnAddDoc">Add New Document</span>
        </h3>
        <div id="frmAddDoc" style="display:none;">
            <form id="frmNewDocument" action="<?php echo plugins_url('new_document.php', dirname(__FILE__)); ?>" method="POST" enctype='multipart/form-data'>
                <input type="hidden" name="post_date" value="<?php echo date('Y-m-d'); ?>">
                <table class="form-table">
                    <tbody>
                        <tr class="form-field form-required">
                            <th scope="row">
                                <label for="groupID">
                                    Select Group <span class="description">(required)</span>
                                </label>
                            </th>
                            <td>
                                <select name="groupID" id="groupID">
                                    <?php foreach($groups as $group): ?>
                                        <option value="<?php echo $group->id; ?>"><?php echo $group->name; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                        </tr>
                        <tr class="form-field form-required">
                            <th scope="row">
                                <label for="document">
                                    Document Name <span class="description">(required)</span>
                                </label>
                            </th>
                            <td>
                                <input type="text" id="document" name="document" value aria-required="true" />
                            </td>
                        </tr>
                        <tr class="form-field form-required">
                            <th scope="row">
                                <label for="description">
                                    Description <span class="description">(required)</span>
                                </label>
                            </th>
                            <td>
                                <textarea id="description" name="description" value aria-required="true"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="description">
                                    Upload PDF: <span class="description">(required)</span>
                                </label>
                            </th>
                            <td>
                                <input type="file" name="doc" id="doc" />
                            </td>
                        </tr>

                    </tbody>
                </table>
                <p class="submit">
                    <button href="#" class="btnCancelAdd">Cancel</button>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input class="button button-primary" type="submit" id="btnNewDocument" name="newDocument" value="<?php _e( 'Add New Document' ); ?>" />
                </p>
            </form>

In the root of the plugin folder, I created the new_document.php to handle the upload. I want to create a camelCase name for the .pdf created from the “document” field. Here’s what I have in that file:

<?php
require_once('/public_html/wp-config.php');
global $wpdb;

define("UPLOAD_DIR", '/public_html/wp-content/uploads/gdocs/');

if( !empty( $_FILES['doc'] ) ):
    $uFile = $_FILES['doc'];

     if ($uFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    $post_date = $_POST['post_date'];
    $groupID = $_POST['groupID'];
    $document = $_POST['document'];
    $description = $_POST['description'];

    $d_pieces = explode(' ', $document);
    $new_name = ' ';
    $count = 1;

    foreach ($d_pieces as $piece){
        if ($count !== 1){
            $piece = ucfirst($piece);
        } // end if
        $newName .= $piece;
        $count++;
    } // end foreach

    $flName = $newName.".pdf";
    $fileName = strtolower( $flName{0}) . substr($flName,1);

    $success = move_uploaded_file($uFile['tmp_name'], UPLOAD_DIR . $fileName);
else:
    echo "No File Uploaded.";
endif;

$wpdb->insert( 'ppm_group_documents', array( 'groupID' => $groupID, 'post_date' => $post_date, 'document' => $document, 'description' => $description, 'path' => $fileName ), array( '%s', '%s', '%s', '%s', '%s' ) );

header('Location: http://[sitename]/wp-admin/admin.php?page=ppm-doc-viewer');
?>

—UPDATE—

The code above works but it is a PHP only rather than WordPress solution. I hope someone could point me to a book or article explaining how to uploads files the “right way” in an OOP plugin.

—END UPDATE–

I am only getting the white screen of death and through FTP, I checked the upload folder and the file isn’t there. Since this is a live site, I can’t turn on debugging.

I am hoping someone will spot something I am doing incorrectly but would also like it if someone could point me to a book or article explaining how to uploads files the “right way” in an OOP plugin.

Thanks in Advance!!!

Related posts