Let’s presume your FILE variable is called ‘files’ and can contain multiple inputs (i.e. name=”files[]”)
1) First upload the files
$uploads = wp_upload_dir();
$upload_file_url = array();
foreach ($_FILES['files']['name'] as $key => $value_data)
{
$errors_founds="";
if ($_FILES['files']['error'][$key] != UPLOAD_ERR_OK)
$errors_founds .= 'Error uploading the file!<br />';
if (!array_key_exists($_FILES['files']['type'][$key], $mime))
$errors_founds .= 'Invalid file type!<br />';
if ($_FILES['files']['size'][$key] == 0)
$errors_founds .= 'Image file it\'s empty!<br />';
if ($_FILES['files']['size'][$key] > 524288)
$errors_founds .= 'Image file to large, maximus size is 500Kb!<br />';
if(!is_uploaded_file($_FILES['files']['tmp_name'][$key]))
$errors_founds .= 'Error uploading the file on the server!<br />';
if ($errors_founds == '')
{
//Sanitize the filename (See note below)
$remove_these = array(' ','`','"','\'','\\',"https://wordpress.stackexchange.com/");
$newname = str_replace($remove_these,'', $_FILES['files']['name'][$key]);
//Make the filename unique
$newname = time().'-'.$newname;
//Save the uploaded the file to another location
$upload_path = $uploads['path'] . "/$newname";
move_uploaded_file($_FILES['files']['tmp_name'][$key], $upload_path);
$upload_files_url[$upload_path] = $uploads['url'] ."/$newname";
}
}
2) Process the successfully uploaded files
if (count($upload_files_url) > 0)
{
foreach ($upload_files_url as $filename_path => $upload_file_url)
{
$wp_filetype = wp_check_filetype(basename($filename_path), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename_path)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename_path);
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
}