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.

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.

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!!!

share|improve this question
1  
why not set up a local install and test your plugin there? developing without debugging is honestly a bit ridiculous. –  Milo Dec 30 '13 at 16:35
    
I am actually setting up a local dev with DesktopServer right now. I didn't realize until I got into it how foreign it is to add upload functionality. –  jgravois Dec 30 '13 at 17:00
    
your code is against wordpress best practices, you basically just ignore the whole wordpress stack, so the question is why do you think that the fact your code is not working is wordpress related? I don't see anything wordpress related in your form handling. And your problem is probably that there no directory called "/publich_html" –  Mark Kaplun Dec 30 '13 at 17:19
    
That is why I posted this question. I don't know HOW to use the WordPress stack to upload documents in an OOP plugin and I can't find anything to show me how on the Web, So I turned to the WP community. I realize that this is a pure PHP solution but even the Codex didn't give me any better guidance. It is also the reason why I asked for a resource if anyone knew any because I want to do it correctly but aside from uploading images or using the Media Uploader, I haven't been able to find anything to help. –  jgravois Dec 30 '13 at 17:27
    
I have updated my code above for the new_document.php file which sucessfully uploads the document and saves the database record. I know this is a pure PHP solution and I want to modify it to WordPress best practices, so hopefully someone will post exactly how I can do that. –  jgravois Dec 30 '13 at 19:38
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.