Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add_action( 'gform_pre_process_178', function ( $form ) {
GFCommon::log_debug( 'gform_pre_process: running' );
$base64_string = rgpost( 'input_3' );
if ( ! empty( $base64_string ) ) {
GFCommon::log_debug( 'gform_pre_process: found string' );
$target_dir = GFFormsModel::get_upload_path( $form['id'] ) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
if ( ! is_dir( $target_dir ) ) {
GFCommon::log_debug( 'gform_pre_process: creating tmp folder' );
if ( ! wp_mkdir_p( $target_dir ) ) {
GFCommon::log_debug( "gform_pre_process: Couldn't create the tmp folder: " . $target_dir );
return;
} else {
GFCommon::recursive_add_index_file( $target_dir );
}
}
$upload_field_id = 4;
$file_extension = 'png';
$file_contents = base64_decode( preg_replace( '#^data:image/\w+;base64,#i', '', $base64_string ) );
$temp_filename = sprintf( '%s_input_%s.%s', GFFormsModel::get_form_unique_id( $form['id'] ), $upload_field_id, $file_extension );
$result = file_put_contents( $target_dir . $temp_filename, $file_contents );
GFCommon::log_debug( 'gform_pre_process: file_put_contents result: ' . var_export( $result, true ) );
$uploaded_file_name = 'testing.png';
$_POST['gform_uploaded_files'] = json_encode( array( 'input_' . $upload_field_id => $uploaded_file_name ) );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment