$ _FILES在WordPress文件上传功能中返回NULL

时间:2015-09-11 10:57:51

标签: php wordpress file-upload

所以我仍然在为我的主题构建设置页面,而且我忙于文件上传功能。 wp_handle_upload函数正在运行,但文件没有被抓取。

这是注册选项:

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

这是设置主题页面的功能:

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
}

这是接受图像的功能:

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
}

这是处理上传的功能:

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';      
}

当我在变量var_dump$uploadedfile时,我得到NULL。那是为什么?

以下是我的主题设置页面的屏幕截图:http://pasteboard.co/yFck7LW.png

这是我尝试上传内容时上传的空文件:http://pasteboard.co/yFhrUbB.png

请帮忙!

1 个答案:

答案 0 :(得分:2)

问题在于:

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

将其更改为:

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