Skip to content

Instantly share code, notes, and snippets.

@saltnpixels
Created December 17, 2020 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltnpixels/25c8752a8d4fb2de63912bfacc33f429 to your computer and use it in GitHub Desktop.
Save saltnpixels/25c8752a8d4fb2de63912bfacc33f429 to your computer and use it in GitHub Desktop.
Gravity Forms Validate Uploaded File
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
foreach ( $form['fields'] as $field ) {
if ( strpos( $field->cssClass, 'myfileclass' ) !== false ) {
$input = 'input_' . $field->id;
$uploaded_files = json_decode( rgpost( "gform_uploaded_files" ) );
$is_file_uploaded = isset( $uploaded_files->$input );
$filename = '';
//file got uploaded while validating other fields
if ( $is_file_uploaded ) {
write_log( 'file was uploaded by gravity forms already' );
$temp_file = GFFormsModel::get_temp_filename($form['id'], $input);
if(isset($temp_file['temp_filename'])){
$file = $temp_file['temp_filename'];
$upload_path = GFFormsModel::get_upload_path( $form['id'] );
$filename = $upload_path . '/tmp/' . $file;
}
} else {
//file was not uploaded yet by gf
if ( ! empty( $_FILES[ $input ]["name"] ) ) {
write_log( 'file not uploaded yet. file in tmp' );
$filename = $_FILES[ $input ]["tmp_name"];
}
}
//we should have a have filename now.
//now do something with the $filename. Check it or whatever and validate
//$validation_result['is_valid'] = false;
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment