Tribe_Meta_Box::save_field_file( $post_id,  $field,  $old,  $new )


Source

File: lib/tribe-meta-box.php

	public function save_field_file( $post_id, $field, $old, $new ) {
		$name = $field['meta'];
		if ( empty( $_FILES[ $name ] ) ) {
			return;
		}

		self::fix_file_array( $_FILES[ $name ] );

		foreach ( $_FILES[ $name ] as $position => $fileitem ) {
			$file = wp_handle_upload( $fileitem, array( 'test_form' => false ) );

			if ( empty( $file['file'] ) ){
				continue;
			}

			$filename = $file['file'];

			$attachment = array(
				'post_mime_type' => $file['type'],
				'guid' => $file['url'],
				'post_parent' => $post_id,
				'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
				'post_content' => '',
			);
			$id = wp_insert_attachment( $attachment, $filename, $post_id );
			if ( ! is_wp_error( $id ) ) {
				wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $filename ) );
				add_post_meta( $post_id, $name, $id, false );	// save file's url in meta fields
			}
		}
	}