微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

“不能使用字符串偏移量作为数组”致命错误

如何解决“不能使用字符串偏移量作为数组”致命错误

我已经看到了类似问题的答案,但它们并没有帮助解决问题。任何帮助是极大的赞赏。谢谢!

问题线:

$postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'],'file' => $file['file'] );

完整代码

function cp_process_new_image() {
global $wpdb;
$postvals = '';

for ( $i=0; $i < count( $_FILES['image']['tmp_name'] ); $i++ ) {
    if ( !empty($_FILES['image']['tmp_name'][$i]) ) {
        // rename the image to a random number to prevent junk image names from coming in
        $renamed = mt_rand( 1000,1000000 ).".".mastheme_find_ext( $_FILES['image']['name'][$i] );

        //Because WP can't handle multiple uploads as of 2.8.5
        $upload = array( 'name' => $renamed,'type' => $_FILES['image']['type'][$i],'tmp_name' => $_FILES['image']['tmp_name'][$i],'error' => $_FILES['image']['error'][$i],'size' => $_FILES['image']['size'][$i] );

        // need to set this in order to send to WP media
        $overrides = array( 'test_form' => false );

        // check and make sure the image has a valid extension and then upload it
        $file = cp_image_upload( $upload );

        if ( $file ) // put all these keys into an array and session so we can associate the image to the post after generating the post id
            $postvals['attachment'][$i] = array( 'post_title' => $renamed,'file' => $file['file'] );
    }
}
return $postvals;

}

解决方法

“不能将字符串偏移用作数组”致命错误

如您的代码所示,$postvals 是字符串而不是数组,因此附件键不存在。

变化:

$postvals = '';

以下之一:

$postvals = array();
$postvals = [];

将变量初始化为正确的类型。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。