Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress用户上传前端:错误非法字符串偏移,文件为空。在后端工作。_Wordpress_Upload_Frontend - Fatal编程技术网

Wordpress用户上传前端:错误非法字符串偏移,文件为空。在后端工作。

Wordpress用户上传前端:错误非法字符串偏移,文件为空。在后端工作。,wordpress,upload,frontend,Wordpress,Upload,Frontend,因此,无法通过前端上传文件并正确保存。 有趣的是,当我通过后端用户面板时,它工作得很好 值得注意的是,这些都是在我的本地主机上运行的MAMP apache 我确保包含正确的文件并声明表单的enctype。我设法使纯文本字段保存得很好。我想我把这部分搞糟了: if ( !empty( $_POST['map_pdf'] ) ) update_user_meta( $current_user->ID, 'map_pdf', wp_handle_upload( $_POST['

因此,无法通过前端上传文件并正确保存。 有趣的是,当我通过后端用户面板时,它工作得很好

值得注意的是,这些都是在我的本地主机上运行的MAMP apache

我确保包含正确的文件并声明表单的enctype。我设法使纯文本字段保存得很好。我想我把这部分搞糟了:

     if ( !empty( $_POST['map_pdf'] ) )
    update_user_meta( $current_user->ID, 'map_pdf', wp_handle_upload( $_POST['map_pdf'], array( 'test_form' => false ) ));     
我已经看到了其他几种解决方案,但这将与用户信息一起显示

以下是我遇到的错误:

Illegal string offset 'size' in /wp-admin/includes/file.php on line 272

File is empty. Please upload something more substantial. 
This error could also be caused by uploads being disabled in your php.ini 
or by post_max_size being defined as smaller than upload_max_filesize in php.ini.    
个人资料页

<?php
/**
 * Template Name: User Profile
 *
 * Allow users to update their profiles from Frontend.
 *
 */



/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();

/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
$error = array();    
/* If profile was saved, update profile. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {

    /* Update user password. */
    if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
        if ( $_POST['pass1'] == $_POST['pass2'] )
            wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
        else
            $error[] = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
    }

    /* Update user information. */
    if ( !empty( $_POST['url'] ) )
       wp_update_user( array ('ID' => $current_user->ID, 'user_url' => esc_attr( $_POST['url'] )));
    if ( !empty( $_POST['email'] ) ){
        if (!is_email(esc_attr( $_POST['email'] )))
            $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
        elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');
        else{
            wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
        }
    }

    $r = get_user_meta( $user->ID, 'map_pdf', true );

    if ( !empty( $_POST['first-name'] ) )
        update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
    if ( !empty( $_POST['last-name'] ) )
        update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
    if ( !empty( $_POST['display_name'] ) )
        wp_update_user(array('ID' => $current_user->ID, 'display_name' => esc_attr( $_POST['display_name'] )));
      update_user_meta($current_user->ID, 'display_name' , esc_attr( $_POST['display_name'] ));
    if ( !empty( $_POST['description'] ) )
        update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );
    if ( !empty( $_POST['map_pdf'] ) )
        update_user_meta( $current_user->ID, 'map_pdf', wp_handle_upload( $_POST['map_pdf'], array( 'test_form' => false ) )); 




    /* Redirect so the page will show updated info.*/
  /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
    if ( count($error) == 0 ) {
        //action hook for plugins and extra fields saving
        do_action('edit_user_profile_update', $current_user->ID);
        wp_redirect( get_permalink().'?updated=true' ); exit;
    }       


}

 ?>
 <?php get_template_part('templates/header'); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


    <div class="contentarea tk-proxima-nova">
        <div class="thecontent">
     <h3>Update Information for &quot;<?php echo $current_user->user_login ?>&quot;</h3></br>
                <?php if ( $_GET['updated'] == 'true' ) : ?> <div id="message" class="updated"><p>Your profile has been updated.</p></div> <?php endif; ?>
                <?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>


    <div id="post-<?php the_ID(); ?>">
        <div class="entry-content entry">
            <?php the_content(); ?>
            <?php if ( !is_user_logged_in() ) : ?>
                    <p class="warning">
                        <?php _e('You must be logged in to edit your profile.', 'profile'); ?>
                    </p><!-- .warning -->
            <?php else : ?>
                <?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
                <form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class="form-username">
                        <label for="first-name"><?php _e('First Name', 'profile'); ?></label>
                        <input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-username">
                        <label for="last-name"><?php _e('Last Name', 'profile'); ?></label>
                        <input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" />
                    </p><!-- .form-username -->
                    <p class="form-email">
                        <label for="email"><?php _e('E-mail *', 'profile'); ?></label>
                        <input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" />
                    </p><!-- .form-email -->
                    <p class="form-url">
                        <label for="url"><?php _e('Website', 'profile'); ?></label>
                        <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( 'user_url', $current_user->ID ); ?>" />
                    </p><!-- .form-url -->
                    <p class="form-password">
                        <label for="pass1"><?php _e('Password *', 'profile'); ?> </label>
                        <input class="text-input" name="pass1" type="password" id="pass1" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label for="pass2"><?php _e('Repeat Password *', 'profile'); ?></label>
                        <input class="text-input" name="pass2" type="password" id="pass2" />
                    </p><!-- .form-password -->
                    <p class="form-textarea">
                        <label for="description"><?php _e('Biographical Information', 'profile') ?></label>
                        <textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( 'description', $current_user->ID ); ?></textarea>
                    </p><!-- .form-textarea -->

                    <?php 
                        //action hook for plugin and extra fields
                        do_action('edit_user_profile',$current_user); 


                    ?>
                    <p class="form-submit">
                        <?php echo $referer; ?>
                        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
                        <?php wp_nonce_field( 'update-user' ) ?>
                        <input name="action" type="hidden" id="action" value="update-user" />
                    </p><!-- .form-submit -->
                </form><!-- #adduser -->
            <?php endif; ?>
        </div><!-- .entry-content -->
    </div><!-- .hentry .post -->
    </div><!-- .hentry .post -->
    </div><!-- .hentry .post -->
    <?php endwhile; ?>
<?php else: ?>
    <p class="no-data">
        <?php _e('Sorry, no page matched your criteria.', 'profile'); ?>
    </p><!-- .no-data -->
<?php endif; ?>

<?php get_template_part('templates/footer'); ?>
下面是functions.php

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { 

$r = get_user_meta( $user->ID, 'map_pdf', true );
    ?>

<h3>file</h3>

<table class="form-table">

<tr>
        <th scope="row">file</th>
        <td><input type="file" name="map_pdf" value="" />

            <?php //print_r($r); 
                if (!isset($r['error'])) {
                    $r = $r['url'];
                    echo $r;
                } else {
                    $r = $r['error'];
                    echo $r;
                }
            ?>
        </td>
    </tr>

</table> 



<?php
}

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {


$_POST['action'] = 'wp_handle_upload';
if( $_FILES['map_pdf']['error'] === UPLOAD_ERR_OK ) {
    $r = wp_handle_upload( $_FILES['map_pdf'] );
    update_user_meta( $user_id, 'map_pdf', $r );
} 

}


add_action('user_edit_form_tag', 'make_form_accept_uploads');
function make_form_accept_uploads() {
    echo ' enctype="multipart/form-data"';
}

非常感谢您的帮助。我一直在到处寻找,我也看到过其他类似的问题,但没有一个像我这样通过拉用户ID并将其发回用户页面来解决

我在检查exists1与current_user->ID:

      elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
        $error[] = __('This email is already used by another user.  try a different one.', 'profile');
你想这样做吗

$current_user_id = $current_user->ID;
$post_email = $_POST['email'];
$esc_email = esc_attr($post_email);
$email_exists = email_exists($esc_email);
$user_by_email = get_user_by( 'email', $esc_email );
$email_user_id = isset($user_by_email->data->ID) ? $user_by_email->data->ID : 0;


    }elseif( $email_user_id && ($email_user_id != $current_user_id) ){
        $error[] = __('This email is already used by another user.  try a different one.', 'profile')

var_dump$\文件以查看发生了什么…顺便说一句,文件类型不是$\ POST,而是$\文件。上面的错误是由wp_handle_upload在不包含文件时抛出的…这实际上是一个文本字段吗?我还没有测试这一点,因为我们的项目已经被移动了,但我会在讨论这个问题时给你回复。我感谢你的回复,不,这不是一个文本字段。这是一个上传文件字段@大卫