Creating a multi-file upload form on the front end for attachments

This may not be the most elegant way to do it (I’m not sure if overwriting the $_FILES global is even allowed) but this seems to work:

global $post;
if ($_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 = wp_insert_attachment($file,$post->ID);
            }                                   
        }
    }
}

Leave a Comment