0

我正在尝试上传多个文件并将它们附加到 Wordpress 帖子中。此代码适用于单个图像:

PHP

function insert_attachment($file_handler,$post_id,$setthumb='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');

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

    if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
        return $attach_id;
}

if ($_FILES) {
    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);
}

HTML

<fieldset class="images">
    <label for="images">Images</label>
    <input type="file" name="images" id="images" tabindex="25"/>
</fieldset>

但是我怎样才能让它与多个图像一起工作呢?

我知道我必须添加multiple="multiple"到输入中,输入的名称应该是images[]. 但我肯定有 PHP 脚本的问题。

4

1 回答 1

1

万一将来有人需要这个:我在这里找到了解决方案:

// If we have files
if ( $_FILES )
{
    // Get the upload attachment files
    $files = $_FILES['upload_attachment'];

    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("upload_attachment" => $file);

            foreach ($_FILES as $file => $array)
            {
                $newupload = insert_attachment($file,$post->ID);
            }
        }
    }
}
于 2013-02-12T10:10:43.510 回答