WP file upload

require( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php");
require( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php");

$merchant_id = $_POST['merchant_id'];

$upload_overrides = array( 'test_form' => FALSE );
$uploaded_file = wp_handle_upload($_FILES['logo'], $upload_overrides);

wp_create_thumbnail( $uploaded_file['file'], 150 );

$uploads = wp_upload_dir();

$wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
'post_content' => '',
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_parent' => $merchant_id,
'guid' => $uploads['baseurl'] . '/' . $_FILES['logo']['name']
);
$attachment_id = wp_insert_post( $attachment );
$attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attach_data );
set_post_thumbnail( $merchant_id, $attachment_id );

header("Location: /merchant-profile/?updated=true");