Using $_FILES variable into the same function for uploading images and files

I did override something. I did not validation and only build structured to validation and upload function. You can validate file as you needed.

if ($_FILES)
    {
        // Get the upload attachment files
        $images = $_FILES['moreimages'];
        $errors="";
        foreach ($images['name'] as $key => $value)
        {
            if ($images['name'][$key])
            {
                $image = array(
                    'name' => $images['name'][$key],
                    'type' => $images['type'][$key],
                    'tmp_name' => $images['tmp_name'][$key],
                    'error' => $images['error'][$key],
                    'size' => $images['size'][$key]
                );

                //here I've changed the $_FILES variable into something else
                $_FILES['moreimages'] = $image;
                if( is_wp_error( $moreimages = project_media_handle_upload('moreimages',$pid) ) )
                $errors .= $moreimages->get_error_message();

            }
        }
        // Get the upload attachment files
        $files = $_FILES['morefiles'];
        foreach ($files['name'] as $key => $value)
        {
            if ($files['name'][$key])
            {
                $file = array(
                    'name' => $files['name'][$key],
                    'type' => $files['type'][$key],
                    'tmp_name' => $files['tmp_name'][$key],
                    'error' => $files['error'][$key],
                    'size' => $files['size'][$key],
                    'post_mime_type' => $files['type'][$key]
                );
                $_FILES['morefiles'] = $file;
                if( is_wp_error( $morefiles = project_media_handle_upload( 'morefiles', $pid, 'file' ) ) )
                $errors .= $morefiles->get_error_message();
            }
        }

        if( ! empty( $errors) )
        echo $errors;
    }

function image_and_files_validate_handle( $file ){
    // $file provide all values size, name etc
    // conditional statement here and return every time fixed key with message
    $file['error'] =  'File is invalid';
    return $file;
}

function file_upload_validate_handle( $file ){
    // $file provide all values size, name etc
    // conditional statement here and return every time fixed key with message
    $file['error'] =  $file['name']. ' is invalid format.';
    return $file;
}

add_action( 'project_media_handle_upload', 'save_file_meta_content_id', 10, 2 );

function save_file_meta_content_id( $fid, $type ){
  if( $type == 'file')
  update_post_meta($fid,'is_prj_file','1'); 
}


function project_media_handle_upload( $file_id, $pid, $type= "image" ){

  $action = 'image_upload_action'; //  change custom action name easy to understand

  if( $type !== 'image')
  $action = 'file_upload_action'; 

  if( function_exists('check_upload_size') )
  add_filter(  "{$action}_prefilter", 'check_upload_size'); // wordpress default filter to check upload size

  add_filter(  "{$action}_prefilter", 'image_and_files_validate_handle'); // both conditional validation for images and files
  add_filter(  "file_upload_action_prefilter", 'file_upload_validate_handle');

  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');
  $result =  media_handle_upload( $file_id, $pid, array(), array( 
    'test_form' => false,
    'action' => $action
  ));

  if( ! is_wp_error($result) )
  do_action( 'project_media_handle_upload', $result, $type );   

  return $result;
}