Marco V
Marco V

Reputation: 2653

$_FILES returns NULL in WordPress file upload function

So I'm still building the settings page for my theme and I'm busy with the file upload function. The wp_handle_upload function is working but the file is not being grabbed.

This is the registered option:

add_settings_field("upload_logo", "Upload Logo", "logo_display", "theme-options", "section");
register_setting("section", "upload_logo", "handle_logo_upload");

This is the function that set's up the theme page:

function theme_settings_page() {
    ?>
        <div class="wrap">
        <h1>Theme Panel</h1>
        <form method="post" action="options.php" enctype="multipart/form-data">
            <?php
                settings_fields("section");
                do_settings_sections("theme-options");      
                submit_button(); 
            ?>          
        </form>
        </div>
    <?php
}

This is the function that accepts an image:

function logo_display()
{
    ?>
     <form method="post" action="options.php" enctype="multipart/form-data">
        <input type="file" name="upload_logo" id="upload_logo" value="<?php echo get_option('upload_logo'); ?>"/>         
    </form>
   <?php
}

This is the function that handles the upload:

function handle_logo_upload() {
    if ( !function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }
        $uploadedfile = $_FILES['upload_logo']['submit'];
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
        if ( $movefile ) {
            $wp_filetype = $movefile['type'];
            $filename = $movefile['file'];
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
                'post_mime_type' => $wp_filetype,
                'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            $attach_id = wp_insert_attachment( $attachment, $filename);
            echo '<br>';
            //return $attach_id;
            var_dump($uploadedfile);
            //print_r($uploadedfile);
            //wp_die('end');
        }

    return 'fail';      
}

When I do I var_dump on the variable $uploadedfile, I get NULL. Why is that?

Here is a screenshot of the settings page for my theme: http://pasteboard.co/yFck7LW.png

This is the empty file that is uploaded when I try to upload something: http://pasteboard.co/yFhrUbB.png

Please help!

Upvotes: 0

Views: 2800

Answers (1)

NaijaProgrammer
NaijaProgrammer

Reputation: 2957

Here's the problem:

$uploadedfile = $_FILES['upload_logo']['submit'];

change that to:

$uploadedfile = $_FILES['upload_logo']['name'];

Upvotes: 2

Related Questions