[英]“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.