I edited code
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$mime = $_FILES[$file]['type'];
$filesize = $_FILES[$file]['size'];
$maxsizef = 524288;
if($filesize > $maxsizef) $error_array[] = 'error size, max file size = 500 KB';
if(($mime != 'image/jpeg') && ($mime != 'image/jpg') && ($mime != 'image/png')) $error_array[] ='error type , please upload: jpg, jpeg, png';
$attach_id = media_handle_upload( $file, $post_id );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($post_id,'_thumbnail_id',$attach_id);
}
it’s work.