title: Wordpress image upload from frontend throws 500 internal server error
body: <p>I have a site with a frontend form where users can add new post. The form has some basic post details and an featured image field. The form also has a few more image fields, which are controlled by acf.</p>
  
  <p>Now the issue is when i use images like png everything works fine. But when i use images like jpg, it gives a internal server error. In acf i have added the accepted file formats as "jpg,png,jpeg". But i am not sure how to add file formats for the featured image.</p>
  
  <p>Here is the code i am using. Here the first image will always be the featured image. </p>
  
  <pre><code>        foreach( $_FILES as $file ) {
              if( is_array( $file ) ) {
                  if($count==1){
                      $featured_image_id = upload_user_file( $file );
                  }else{
                      $item_image[] = upload_user_file( $file );
  
                  }
              }
              $count++;
            }
  </code></pre>
  
  <p>And here is the upload_user_file function</p>
  
  <pre><code>function upload_user_file( $file = array() ) {
      require_once( ABSPATH . 'wp-admin/includes/admin.php' );
        $file_return = wp_handle_upload( $file, array('test_form' =&gt; false ) );
        if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
            return false;
        } else {
            $filename = $file_return['file'];
            $attachment = array(
                'post_mime_type' =&gt; $file_return['type'],
                'post_title' =&gt; preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                'post_content' =&gt; '',
                'post_status' =&gt; 'inherit',
                'guid' =&gt; $file_return['url']
            );
            $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
            wp_update_attachment_metadata( $attachment_id, $attachment_data );
            if( 0 &lt; intval( $attachment_id ) ) {
              return $attachment_id;
            }
        }
        return false;
  }
  </code></pre>