Its pretty simple actually. Here is the link for the set_thumbnail function reference codex. And here is the answer for the file upload.
First off all you have to attach the file to the post :
function attach_uploads($uploads,$post_id = 0){
$files = rearrange($uploads);
if($files[0]['name']==''){
return false;
}
foreach($files as $file){
$upload_file = wp_handle_upload( $file, array('test_form' => false) );
$attachment = array(
'post_mime_type' => $upload_file['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_file['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
$attach_array[] = $attach_id;
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
return $attach_array;
}
More detail about the php file upload rearrangement : php.net.
And this is the function to set the uploaded file as post thumbnail. Put it in the corresponding wp_ajax function.
// put this one early, before any process
$files = $_FILES['profile-picture'];
// insert attachment, after you have the new post id of course
$attached_files = attach_uploads($files,$pid);
// set the first file as post thumbnail
// $attached_files[0] is for the first file
if($attached_files){
set_post_thumbnail( $pid, $attached_files[0] );
}
Hope this help
ADDED :
function rearrange( $arr ){
foreach( $arr as $key => $all ){
foreach( $all as $i => $val ){
$new[$i][$key] = $val;
}
}
return $new;
}
Just put it in the functions.php