Manipulate the files uploaded via Gravity Forms before they are stored

I would like to make changes to files (for e.g. compress them into a zip) uploaded via Gravity Forms File upload field before they are uploaded to the webserver.

Do you know of any hook or workaround to modify the (temporary) file before it is sent to and stored on the webserver?

(I have found a solution to do it after the files are uploaded, but that is not what I am looking for.)

Topic plugin-gravity-forms uploads Wordpress

Category Web


Looks like I was using this class to do this: http://gravitywiz.com/rename-uploaded-files-for-gravity-form/ and added the filter in myself after line 207, ie.

    // replace merge tags
    $form  = GFAPI::get_form( $entry['form_id'] );
    $value = GFCommon::replace_variables( $value, $form, $entry, false, true, false, 'text' );

    // MOD: add a filter here to hook into filename change
    $value = apply_filters('gravity_forms_upload_filename', $value, $entry);

$value will then contain the full path to then uploaded file which can be hooked into and zipped so you can return the zip path instead.

Here is the snippet I was using to load GW_Rename_Uploaded_Files:

add_action('init', 'my_forms_rename_uploads', 11);
if (!function_exists('my_forms_rename_uploads')) {
 function my_forms_rename_uploads() {
    if (!class_exists('GW_Rename_Uploaded_Files')) {return;}
    $form_ids = array(
        1, // list of form IDs to load renaming class for
    );

    // --- get all file upload fields ---
    foreach ($form_ids as $form_id) {
        $upload_field_ids = array();
        $form = GFFormsModel::get_form_meta($form_id);
        if (isset($form['fields'])) {
            // --- get upload field IDs ---
            foreach ($form['fields'] as $field) {
                if ($field->type == 'fileupload') {$upload_field_ids[] = $field->id;}
            }
            // --- set class arguments ---
            $args = array(
                'form_id' => $form_id,
                'field_id' => $upload_field_ids,
                'template' => '{filename}'
            );
            // --- initialize rename uploaded files class for fields ---
            new GW_Rename_Uploaded_Files($args);
        }
    }
 }
}

Hope that helps...

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.