Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Created July 9, 2019 12:52
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/17d8ee4285d1c0c82125262b9b4348ec to your computer and use it in GitHub Desktop.
Save richardW8k/17d8ee4285d1c0c82125262b9b4348ec to your computer and use it in GitHub Desktop.
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