Loop over the $_FILES
array and add each file separately. I don’t have a complete code example at hand, just an excerpt from my theme options class:
/**
* Saves uploaded files in media library and the corresponding id in option field.
*
* @return void
*/
protected function handle_uploads()
{
if ( ! isset ( $_FILES ) or empty ( $_FILES ) )
{
return;
}
foreach ( $_FILES as $file_key => $file_arr )
{
// Some bogus upload.
if ( ! isset ( $this->fields[$file_key] )
or empty ( $file_arr['type'] )
)
{
continue;
}
if ( ! $this->is_allowed_mime( $file_key, $file_arr ) )
{
set_theme_mod( $file_key . '_error', 'wrong mime type' );
continue;
}
// The file is allowed, no error until now and the type is correct.
$uploaded_file = wp_handle_upload(
$file_arr
, array( 'test_form' => FALSE )
);
// error
if ( isset ( $uploaded_file['error'] ) )
{
set_theme_mod( $file_key . '_error', $uploaded_file['error'] );
continue;
}
// add the file to the media library
// Set up options array to add this file as an attachment
$attachment = array(
'post_mime_type' => $uploaded_file['type']
, 'post_title' => $this->get_media_name(
$file_key, $uploaded_file['file']
)
);
// Adds the file to the media library and generates the thumbnails.
$attach_id = wp_insert_attachment(
$attachment
, $uploaded_file['file']
);
$this->create_upload_meta( $attach_id, $uploaded_file['file'] );
// Update the theme mod.
set_theme_mod( $file_key, $attach_id );
remove_theme_mod( $file_key . '_error' );
}
}
handle_uploads()
is called by save_options()
from the same class. is_allowed_mime()
guarantees that I get only file the types I need, and get_media_name()
looks for a special predefined name for the file (»Logo«, »bg-body« etc.).
As you can see, it isn’t that hard. Just use the build-in API, not the code from your upload-file.php
. There are many, many edge cases which WordPress will handle better than your plain move_uploaded_file()
.