简体   繁体   English

上传文件PHP时偏移类型非法

[英]Illegal offset type when uploading a file PHP

All, I'm using this input field: 全部,我正在使用此输入字段:

<input type="file" name="image_to_upload_image" accept="image/*">

I'm then trying to process the file upload with the following code: 然后,我尝试使用以下代码处理文件上传:

if ( $_FILES["image_to_upload_image"] ) { 
    echo "it will try and upload";
    $file = $_FILES["image_to_upload_image"];
    print_r($file);
    $newupload = bmt_handle_attachment_new_post($file,$post_id); 
    echo "The newupload is: ".$newupload;
    set_post_thumbnail( $post_id, $newupload );
}

Which calls the following function: 其中调用以下函数:

function bmt_handle_attachment_new_post($file_handler,$post_id,$set_thu=false) {
    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attachment_id = media_handle_upload( $file_handler, $post_id );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

     // If you want to set a featured image from your uploads. 
    return $attachment_id;
}

I'm doing the same thing as this example: http://codex.wordpress.org/Function_Reference/media_handle_upload 我正在执行与此示例相同的操作: http : //codex.wordpress.org/Function_Reference/media_handle_upload

Why is it saying illegal offset type? 为什么说非法胶印类型?

UPDATE: I have another form that has the same input field except it handles multiple file uploads: 更新:我有另一个具有相同输入字段的表单,但它可以处理多个文件上载:

<input type="file" name="kv_multiple_attachments[]"  multiple="multiple" >

If I then pass it to the following form handler: 如果然后将其传递给以下表单处理程序:

if ( $_FILES ) { 
    $files = $_FILES["kv_multiple_attachments"];
    $gallery_id = $_POST['gallery_for_upload'];  
    foreach ($files['name'] as $key => $value) {            
            if ($files['name'][$key]) { 
                $file = array( 
                    'name' => $files['name'][$key],
                    'type' => $files['type'][$key], 
                    'tmp_name' => $files['tmp_name'][$key], 
                    'error' => $files['error'][$key],
                    'size' => $files['size'][$key]
                ); 
                $_FILES = array ("kv_multiple_attachments" => $file); 
                foreach ($_FILES as $file => $array) {                          
                    $newupload = bmt_handle_attachment($file,$pid,$gallery_id); 
                        //$thumb_url = wp_get_attachment_thumb_url( $newupload );
                        //echo "<img src=".$thumb_url.">";
                }
            } 
        } 
    }

Then use the same processing function: 然后使用相同的处理功能:

function bmt_handle_attachment($file_handler,$post_id,$gallery_id,$set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$attachment_id = media_handle_upload( $file_handler, $post_id );
update_post_meta( $attachment_id, 'bmt_gallery_id', $gallery_id );

 // If you want to set a featured image frmo your uploads. 
return $attachment_id;
}

The files are uploaded without an issue. 文件上传没有问题。

Your bmt_handle_attachment function wants the name of the input field as its first parameter. 您的bmt_handle_attachment函数需要输入字段的名称作为其第一个参数。 You are passing it $_FILES["image_to_upload_image"] when you should just be passing "image_to_upload_image" . 当您只应传递"image_to_upload_image"时,您将传递$_FILES["image_to_upload_image"]

$file = "image_to_upload_image";
$newupload = bmt_handle_attachment_new_post($file,$post_id); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM