简体   繁体   English

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

[英]“Cannot use string offset as an array” Fatal Error

I have seen answers to a similar issue but they didn't help resolve the problem.我已经看到了类似问题的答案,但它们无助于解决问题。 Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks!谢谢!

Problem line:问题线:

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

Complete code:完整代码:

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,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );
    }
}
return $postvals;

} }

“Cannot use string offset as an array” Fatal Error “不能将字符串偏移量用作数组”致命错误

As shown in your code, $postvals is a string and not an array, so the key for attachment does not exist.如您的代码所示, $postvals是一个字符串而不是数组,因此附件的键不存在。

Changing:改变:

$postvals = '';

To one of the following:到以下之一:

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

Will initialize the varaible as the correct type.将变量初始化为正确的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:不能将字符串偏移量用作数组? - Fatal error: Cannot use string offset as an array? 致命错误:不能将字符串偏移用作数组 - Fatal error: Cannot use string offset as an array 致命错误:无法将字符串偏移量用作数组 - Fatal error: Cannot use string offset as an array PHP 7 致命错误:未捕获错误:无法使用字符串偏移量作为数组 - PHP 7 Fatal error: Uncaught Error: Cannot use string offset as an array 致命错误:未捕获错误:无法使用字符串偏移量作为数组 - Fatal error: Uncaught Error: Cannot use string offset as an array 使用变量时:致命错误:不能将字符串偏移量用作数组 - when using variable: Fatal error: Cannot use string offset as an array PHP致命错误:无法将字符串偏移量用作数组 - PHP Fatal error: Cannot use string offset as an array 致命错误:不能将字符串偏移用作数组 - 从JSON响应中解析字符串 - Fatal error: Cannot use string offset as an array - parsing string from JSON response 致命错误:无法将字符串偏移量用作数组-比较数组值的正确方法是什么 - Fatal error: Cannot use string offset as an array - What the right way to compare array value 越来越令人讨厌的错误:“致命错误:无法在…中使用字符串偏移量作为数组” - getting really annoying error: “Fatal error: Cannot use string offset as an array in…”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM