Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active July 14, 2021 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richardW8k/7488889 to your computer and use it in GitHub Desktop.
Save richardW8k/7488889 to your computer and use it in GitHub Desktop.
Logs file uploads for single file and multi-file upload fields pre and post submission to the Gravity Forms logging add-on: http://www.gravityhelp.com/downloads/#Gravity_Forms_Logging_Tool
add_action( 'gform_pre_submission_8', 'uploads_log_pre_submission' );
function uploads_log_pre_submission( $form ) {
foreach ( $form["fields"] as &$field ) {
if ( $field["type"] == "fileupload" ) {
$id = "input_".$field["id"];
if ( $field["multipleFiles"] ) {
if ( empty( $_POST["gform_uploaded_files"] ) )
continue;
$uploaded_files = json_decode( stripslashes( $_POST["gform_uploaded_files"] ), true );
GFCommon::log_debug( "Field: {$field["id"]} (multipleFiles) \$_POST - " . print_r( $uploaded_files, true ) );
foreach ($uploaded_files[$id] as $uploaded_file){
$file_name = $uploaded_file["uploaded_filename"];
$file = GFFormsModel::get_file_upload_path( $form["id"], $file_name );
GFCommon::log_debug( "Field: {$field["id"]} (multipleFiles) - {$file["url"]}" );
}
} else {
if ( empty( $_FILES[$id]["name"] ) )
continue;
GFCommon::log_debug( "Field: {$field["id"]} (singleFile) \$_FILES - " . print_r( $_FILES[$id], true ) );
$file_name = $_FILES[$id]["name"];
$file = GFFormsModel::get_file_upload_path( $form["id"], $file_name );
GFCommon::log_debug( "Field: {$field["id"]} (singleFile) - {$file["url"]}" );
}
}
}
}
add_action( 'gform_after_submission_8', 'uploads_log_after_submission', 10, 2 );
function uploads_log_after_submission( $entry, $form ) {
foreach ( $form["fields"] as &$field ) {
if ( $field["type"] == "fileupload" ) {
$id = $field["id"];
if ( empty( $entry[$id] ) )
continue;
if ( $field["multipleFiles"] ) {
$uploaded_files = json_decode( stripslashes( $entry[$id] ), true );
foreach ( $uploaded_files as $uploaded_file ) {
GFCommon::log_debug( "Field: {$id} (multipleFiles) - {$uploaded_file}" );
}
} else {
$uploaded_files = $entry[$id];
GFCommon::log_debug( "Field: {$id} (singleFile) - {$entry[$id]}" );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment