An even easier option is to use my Gravity Forms Media Library plugin. Here’s the meat and potatoes of the functionality:
public function maybe_upload_to_media_library( $entry, $form ) {
$has_change = false;
foreach( $form['fields'] as $field ) {
if( ! $this->is_applicable_field( $field ) ) {
continue;
}
$value = $entry[ $field->id ];
if( $field->multipleFiles ) {
$value = json_decode( $value );
}
if( empty( $value ) ) {
continue;
}
$has_change = true;
$ids = $this->upload_to_media_library( $value, $field, $entry );
$new_value = array();
if( is_wp_error( $ids ) ) {
continue;
}
foreach( $ids as $id ) {
if( ! is_wp_error( $id ) ) {
$new_value[] = wp_get_attachment_url( $id );
}
}
if( $field->multipleFiles ) {
$new_value = json_encode( $new_value );
} else {
$new_value = $new_value[0];
$ids = $ids[0];
}
$entry[ $field->id ] = $new_value;
$this->update_file_ids( $entry['id'], $field->id, $ids );
}
if( $has_change ) {
GFAPI::update_entry( $entry );
}
return $entry;
}
But… it also handles a ton of other use-cases you may not be considering such as updating the entry, multi-file uploads, and auto-integration with ACF various image-based custom field types.