0

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.)

  • not sure but maybe you could try using gravity_forms_upload_filename filter to trigger the zipping and change the filename – majick Oct 9 at 12:33
  • @majick according to docs.gravityforms.com search "No results were found for "gravity_forms_upload_filename"." Tried with "gravity_form_upload_filename" as well. Can you please provide a link ? – lesDev Oct 9 at 15:16
  • sorry my bad, I didn't check that properly. I took that from a project customization file - it looked like a standard filter...well since I put my foot in it, I'll write an answer. – majick Oct 10 at 2:55
0

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...

  • "$value will then contain the full path to then uploaded file" – so the file is (will be) already on the server when you start processing (rename, compress, etc.) ? – lesDev Oct 10 at 13:14
  • I believe so, I think they are written when the form is processed and this hooks in after that, but not 100% sure without trawling through Gravity Forms code. – majick Oct 10 at 13:34
  • if the filename filter doesn't work where it is (ie. if the value is not actually the file's file path), then you can move that filter into the increment_file function of the class, the full filename path is definitely available there via $file_path = GFFormsModel::get_physical_file_path( $file ); – majick Oct 10 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.