thanks!
here is the script in case someone else is looking for something like this
$value is the filename
$save_path = "folder/";
$file = realpath($save_path).'/'.$value;
$wp_filetype = wp_check_filetype(basename($file), null );
// Construct the attachment array
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'guid' => $file,
'post_title' => preg_replace('/\.[^.]+$/', '', basename($post_name)),
'post_content' => '',
'post_parent' => $post_id,
'post_status' => 'inherit'
);
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_id);
$attach_data = wp_generate_attachment_metadata( $id, $file );
wp_update_attachment_metadata( $id, $attach_data );
set_post_thumbnail( $post_id, $id );