GF_Field_FileUpload

Introduction

The GF_Field_FileUpload class extends the GF_Field class, also known as the Field Object. This class is responsible for determining how the File Upload field is rendered when the form is displayed and how its value is handled during and after form submission.

Settings and Properties

Settings control what options are available to the admin user when configuring the field in the form editor. Gravity Forms includes many built-in settings such as Field Label, Field Description, Choices, Conditional Logic, etc. In addition to built-in settings, custom settings can also be developed. For more information on how to develop custom settings and how to associate settings to a field, visit the GF_Field page.

Properties contain the values specified by the settings and generally are part of the Field Object.

The properties may be retrieved by accessing the Field Object as follows:

// Get the field.
$field = GFAPI::get_field( $form_or_id, $field_id );

// Get the allowed extensions.
$extensions = $field->allowedExtensions;

Settings

The following settings are available for the field:

  • admin_label_setting
    Controls whether the “Admin Field Label” setting appears.

  • conditional_logic_field_setting

    Controls whether the “Enable Conditional Logic” setting appears.

  • css_class_setting

    Controls whether the “Custom CSS Class” setting displays. This allows a custom CSS to be used for the field.

  • description_setting

    Controls whether the “Description” setting appears. This allows a description for the field to be displayed.

  • error_message_setting

    Controls whether the “Custom Validation Message” setting which allows a custom message to be used appears.

  • file_extensions_setting

    Determines whether the “Allowed file extensions” section displays. This section allows the user to enter a comma-delimited list of file extensions which may be uploaded

  • file_size_setting

    Determines whether the “Maximum File Size” section displays. This section sets the allowed size for each file uploaded.

  • label_setting

    Controls whether the “Field Label” setting which allows the label to be changed appears.

  • multiple_files_setting

    Determines whether the “Enable Multi-File Upload” section displays. This section allows the interface to be changed to allow multiple files to be uploaded, instead of a single upload. This section also includes the ability to set a limit on how many files may be uploaded.

  • rules_setting

    Controls whether the “Rules” settings section displays. The “Required” option shows when this is active.

  • visibility_setting

    Controls whether the “Visibility” setting displays. The controls whether the option of visibility being for “Everyone” or “Admin Only” can be set.

Properties

Below is a listing of the properties inherited from the parent class, and the properties unique to the field.

  • adminLabel string

    The label to be used on admin pages instead of the label, useful for fields with long labels.

  • adminOnly boolean

    Determines whether the field is visible to the user submitting the form, or only visible in the admin.

  • allowedExtensions string

    A comma-delimited list of the file extensions which may be uploaded.

  • conditionalLogic array

    An associative array containing the conditional logic rules. See the Conditional Logic Object for more details.

  • cssClass string

    The custom CSS class or classes to be added to the input tag for the field.

  • description string

    The field description.

  • descriptionPlacement string

    The placement of the field description. The description may be placed “above” or “below” the field inputs. If the placement is not specified, then the description placement setting for the Form Layout is used.

  • errorMessage string

    The custom error message to be displayed if the field fails validation.

  • formId integer

    The form ID.

  • id integer

    The field ID.

  • isRequired boolean

    Marking the field as required will prevent the form from being submitted if the user does not enter a value. Default is false.

  • label string

    The field label that will be displayed on the form and on the admin pages.

  • maxFiles string

    When the field is set to allow multiple files to be uploaded, this property is available to set a limit on how many may be uploaded.

  • maxFileSize integer

    The maximum size an uploaded file may be.

  • multipleFiles boolean

    Indicates whether multiple files may be uploaded and changes the interface accordingly.

  • type string

    The field type, which in this case is fileupload.

Dynamic Population

The File Upload field can’t currently be used with dynamic population.

Calculations Support

The File Upload field can’t currently be used with calculations.

Conditional Logic Support

Conditional logic rules can’t currently be based on the File Upload field.

Draft Entry Value

Since version 2.8.6, the field value found in the draft entry, returned by GFFormsModel::create_lead() and GFFormsModel::get_current_lead(), contains a JSON encoded array of details about the temporary file(s). Example:

[{"tmp_path":"\/public\/wp-content\/uploads\/gravity_forms\/6-13631115a0dd7370720e7c2979c2f768\/tmp\/65febb4be9e37_input_3.png","tmp_url":"https:\/\/domain.local\/wp-content\/uploads\/gravity_forms\/6-13631115a0dd7370720e7c2979c2f768\/tmp\/65febb4be9e37_input_3.png","tmp_name":"65febb4be9e37_input_3.png","uploaded_name":"screenshot.png"}]

This is the same for both the single file field and the multi-file enabled field. The array for each file contains the following:

  • tmp_path string
    The server path of the file located in the forms tmp directory.
  • tmp_url string
    The URL of the file located in the forms tmp directory.
  • tmp_name string
    The temporary name assigned to the file.
  • uploaded_name string
    The name of the file uploaded by the form submitter.

To access these file properties, you’ll need to JSON decode the field entry value. Example:

$field_value = rgar( $draft_entry, $field_id );
if ( empty( $field_value ) ) {
	return;
}

$files = json_decode( $field_value, true );
if ( empty( $files ) ) {
	return;
}

This will provide you with an indexed array, which can be accessed like so:

foreach ( $files as $file ) {
	// Do something with the file.
	$tmp_path      = rgar( $file, 'tmp_path' );
	$tmp_url       = rgar( $file, 'tmp_url' );
	$tmp_name      = rgar( $file, 'tmp_name' );
	$uploaded_name = rgar( $file, 'uploaded_name' );
}

They can also be accessed using the file index instead of a foreach. The following example is accessing the first file, which is located at index 0.

$tmp_path      = rgars( $files, '0/tmp_path' );
$tmp_url       = rgars( $files, '0/tmp_url' );
$tmp_name      = rgars( $files, '0/tmp_name' );
$uploaded_name = rgars( $files, '0/uploaded_name' );

If you will be using the URL of the temporary file publicly, you might want to hide the real location of the file by using the field’s get_download_url() method to return a URL using a gf-download query string instead, e.g.

$field    = GFAPI::get_field( $form_or_id, $field_id );
$file_url = $field->get_download_url( $tmp_url, $force_download = false );

Saved Entry Value

The saved entry value depends on how the field is configured. For the default single file configuration, the value is the URL of the uploaded file. For the multi-file configuration, the value is a JSON encoded array of file URLs.

We recommend using the following approach to access the URL(s).

$field_value = rgar( $entry, $field_id );
if ( empty( $field_value ) ) {
	return;
}

$urls = json_decode( $field_value, true );
if ( empty( $urls ) ) {
	$urls = array( $field_value );
}

foreach ( $urls as $url ) {
	// Do something with the $url.
}

Source Code

The source code is located in includes/fields/class-gf-field-fileupload.php in the Gravity Forms folder of your site’s plugins directory.