[英]“Cannot use string offset as an array” Fatal Error
我已經看到了類似問題的答案,但它們無助於解決問題。 任何幫助是極大的贊賞。 謝謝!
問題線:
$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,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );
}
}
return $postvals;
}
“不能將字符串偏移量用作數組”致命錯誤
如您的代碼所示, $postvals
是一個字符串而不是數組,因此附件的鍵不存在。
改變:
$postvals = '';
到以下之一:
$postvals = array();
$postvals = [];
將變量初始化為正確的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.