在MEDIA_HANDLE_UPLOAD之后无法移动文件

时间:2015-04-28 作者:pukka

我正在尝试使用media\\u handle\\u upload()为自定义帖子类型创建元图像上载程序,但它只会返回一个错误,告诉我它无法将图像移动到文件夹“wp content/year/month”-常规上载可以正常工作,不应该存在目录权限问题。。。

我之前使用的是wp\\U insert\\U附件,但由于某种原因,我的上载在媒体库中没有缩略图,而且wp Codex声明我应该使用media\\U handle\\U upload来保存其他函数调用。有人能帮忙吗?这是我的代码:

/// SAVE MITARBEITER META DATA
function mitarbeiter_save_post_meta($post_id,$post){
    /// GET THE POST TYPE OBJECT
    $post_type = get_post_type_object( $post->post_type );

    /// CHECK IF THE CURRENT USER HAS THE NEEDED PERMISSION
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /// PREVENT CLEARING THE CUSTOMFIELDS BY AUTOSAVE, QUICK EDIT OR BULK EDIT
    if ((defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) || (defined(\'DOING_AJAX\') && DOING_AJAX) || isset($_REQUEST[\'bulk_edit\']))
        return;

    /// UPDATE DATA
    $mitarbeiter_leitung = (isset($_POST[\'mitarbeiter_leitung\'])) ? $_POST[\'mitarbeiter_leitung\'] : "";
    $mitarbeiter_email = (isset($_POST[\'mitarbeiter_email\'])) ? $_POST[\'mitarbeiter_email\'] : "";
    $mitarbeiter_position = (isset($_POST[\'mitarbeiter_position\'])) ? $_POST[\'mitarbeiter_position\'] : "";
    $mitarbeiter_position_extra = (isset($_POST[\'mitarbeiter_position_extra\'])) ? $_POST[\'mitarbeiter_position_extra\'] : "";
    $mitarbeiter_position_EN = (isset($_POST[\'mitarbeiter_position_EN\'])) ? $_POST[\'mitarbeiter_position_EN\'] : "";
    $mitarbeiter_position_extra_EN = (isset($_POST[\'mitarbeiter_position_extra_EN\'])) ? $_POST[\'mitarbeiter_position_extra_EN\'] : "";
    update_post_meta( $post_id, \'mitarbeiter_leitung\', $mitarbeiter_leitung );
    update_post_meta( $post_id, \'mitarbeiter_email\', $mitarbeiter_email );
    update_post_meta( $post_id, \'mitarbeiter_position\', $mitarbeiter_position );
    update_post_meta( $post_id, \'mitarbeiter_position_extra\', $mitarbeiter_position_extra );
    update_post_meta( $post_id, \'mitarbeiter_position_EN\', $mitarbeiter_position_EN );
    update_post_meta( $post_id, \'mitarbeiter_position_extra_EN\', $mitarbeiter_position_extra_EN );

    /// CUSTOM IMAGE UPLOAD
    if(!empty($_FILES[\'custom_upload\'][\'name\'])){ //New upload
        require_once( ABSPATH . \'wp-admin/includes/file.php\' );
        $override[\'action\'] = \'editpost\';
        $uploaded_file = wp_handle_upload($_FILES[\'custom_upload\'], $override);
        $post_id = $post->ID;
        $attachment = array(
            \'post_title\' => $_FILES[\'custom_upload\'][\'name\'],
            \'post_content\' => \'\',
            \'post_type\' => \'attachment\',
            \'post_parent\' => $post->ID,
            \'post_mime_type\' => $_FILES[\'custom_upload\'][\'type\'],
            \'guid\' => $uploaded_file[\'url\']
        );
        #$id = wp_insert_attachment( $attachment,$_FILES[\'custom_upload\'][ \'file\' ],$post->ID);
        #wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $_FILES[\'custom_upload\'][\'file\'] ) );
        $id = media_handle_upload(\'custom_upload\',$post->ID);
        update_post_meta($post->ID, "custom_upload",$id);
    }
}
add_action( \'save_post\', \'mitarbeiter_save_post_meta\', 10, 2 );

/// MAKE FILEUPLOAD POSSIBLE
function fileupload_metabox_header(){
    ?>
    <script>
    jQuery(document).ready(function(){
        // Das ist ein Test
        jQuery("form#post").attr("enctype","multipart/form-data");
        jQuery("form#post").attr("encoding","multipart/form-data");
    });
    </script>
    <?php
}
add_action(\'admin_head\', \'fileupload_metabox_header\');

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

你做了两次-media_handle_upload 是的包装器wp_handle_upload.

只需像这样使用前者:

require_once ABSPATH . \'wp-admin/includes/file.php\';
$id = media_handle_upload(
    \'custom_upload\',
    $post->ID,
    array(), // Attachment post data overrides, WordPress will do all the grunt work by default
    array(
        \'test_form\' => false,
        \'action\' => \'editpost\',
    )
);

if ( ! is_wp_error( $id ) )
    update_post_meta( $post->ID, \'custom_upload\', $id );

结束

相关推荐