Has anyone integrated Forminator's Upload field with Dropbox

Hi, I have to build a form where users will submit photos, large print resolution photos, along with other information about themselves. However, I'm concerned that this will fill up my hosting environment since the pictures, by default, are stored in the uploads folder.

My hope is to integrate with Dropbox so that instead of uploading to the WordPress uploads folder, Forminator would upload the attachment to a Dropbox folder, which is synced on my desktop.

I have turned on the Zapier integration and have successfully added an attachment to the Dropbox folder by way of form submission. However, it seems that the file is also stored in the Uploads folder also.

Is there a way to pass the attachment on to Dropbox without storing the file in Wordpress?

  • Adam Czajczyk
    • Support Gorilla

    Hi Zach

    I hope you're well today!

    The Zapier is the preferred way to actually "forward" file to the Dropbox or other services (it can also support Google Drive the same way Remik Woroniecki ) so that's a suggested solution.

    As for "skipping local upload" though - that's a bit more complex. The way any upload is "by default" handles by WordPress (and actually PHP) is that the file is uploaded to the local server, to the temp folder, from where it's copied to some "real" location on the server. "Piping" it directly to the Dropbox or Google Drive wouldn't be that simple - if possible at all - and would also seriously slow down form submission, especially in case of bigger files.

    However, a common workaround for this would be to actually delete the file from a local folder on a server after its "offloaded" to external location. This is, for example, how Snapshot does it with snapshots stored in remote locations - file is uploaded in background and then removed after that.

    I admit, however, that I'm not sure how this - technically speaking - would be possible in case of Zapier integrations so I have asked our developers for consultation on this to see if we could possibly implement this in future :slight_smile:

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    HI Zach

    I just got some additional information. It seems that unlike I expected it's not easy to "detect" when Zappier successfully transfers the files as what we do is actually triggering Zappier action via webhooks.

    However, there's another idea that might work for you in my opinion. What if you could set some "scheduled clean up"? I'm thinking about a solution where you just set that e.g. all files uploaded by Forminator or its particular form that are, for example, a day or week or month old are just automatically removed from your server.

    Could that work for you? I would ask our developers again if they could provide us with some custom code for you to do this so if you think that could do the job for you, let me know please. I'll then move this ticket to our support forum and ask them if that would be possible and if they could help us achieve that.

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hi Zach

    Thank you for your response!

    Since it seems such "clean-up" would be an acceptable solution for you, I took a liberty of moving this thread to our support forum, setting is as a support ticket. I've asked our developers to look into it and see if they could come up with some additional code that would be able to perform such "schedule clean-up".

    I believe they should be able to help us with that but I'd also appreciate some patience on it. It's a bit of custom code that goes more into a "custom feature" area rather than "fixing an issue" so it might take a bit more time for them to handle that. But they'll look into it and update us here as soon as they come up with something :slight_smile:

    Best regards,
    Adam

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Zach ,

    I've created a mu-plugin to see if I can help with what you're trying to achieve. Please read carefully on how to use it and what it will do at the moment to avoid any confusion and unfortunate events since we're talking about deleting files.

    How to install:
    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Inside the mu-plugins folder create a file named forminator-delete-files-schedule.php
    3] Edit the file and copy / paste this code snippet inside.

    <?php
    
    /**
     * Plugin Name: Forminator - Delete uploaded files on schedule.
     * Plugin URI: https://premium.wpmudev.org/
     * Description: mu-plugin for deleting form attached files.
     * Version: 1.0.0
     * Author: Konstantinos Xenos @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( ! class_exists( 'WPMUDEV_Forminator_Delete_Files_Schedule' ) ) {
    	/**
    	 * WPMUDEV_Forminator_Delete_Files_Schedule Class
    	 */
    	class WPMUDEV_Forminator_Delete_Files_Schedule {
    
    		/**
    		 * The Form ID.
    		 * Edit number 5 to the Form ID that you like to check.
    		 *
    		 */
    		private static $_form_id = 5;
    
    		// DO NOT EDIT AFTER THIS LINE.
    
    		/**
    		 * Constructor.
    		 */
    		public function __construct() {
    			add_action(
    				'init',
    				array( $this, 'hook_scheduled_event' )
    			);
    
    			add_action(
    				'wpmudev_forminator_del_files',
    				array( $this, 'remove_files_entries' )
    			);
    		}
    
    		/**
    		 * Adds the scheduled event.
    		 *
    		 * @return void
    		 */
    		public function hook_scheduled_event() {
    			if ( ! wp_next_scheduled( 'wpmudev_forminator_del_files' ) ) {
    				wp_schedule_event( time(), 'daily', 'wpmudev_forminator_del_files' );
    			}
    		}
    
    		/**
    		 * Removes the uploaded files.
    		 *
    		 * @return void
    		 */
    		public function remove_files_entries() {
    			// Load the checked entries.
    			$deleted_attachments = maybe_unserialize( get_option( 'custom_frm_del_att', array() ) );
    
    			// Query the database for form entries.
    			global $wpdb;
    
    			$results = $wpdb->get_results(
    				$wpdb->prepare(
    					"SELECT entry_id, form_id
    					FROM {$wpdb->prefix}frmt_form_entry
    					WHERE date_created + INTERVAL 7 DAY < NOW()
    					AND form_id = %d",
    					self::$_form_id
    				)
    			);
    
    			// Loop through the results to see if we have attachments.
    			foreach ( $results as $result ) {
    
    				// If the entry is not in $deleted_attachments then we haven't deleted a file for it.
    				if ( ! in_array( $result->entry_id, $deleted_attachments, true ) ) {
    					$entry = Forminator_API::get_entry( self::$_form_id, $result->entry_id );
    
    					foreach ( $entry->meta_data as $meta ) {
    						if ( isset( $meta['value']['file']['file_path'] ) && file_exists( $meta['value']['file']['file_path'] ) ) {
    							// Add an error log line if a files is deleted and debug is on.
    							if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
    								error_log( 'Deleting uploaded image to forminator from path: ' . $meta['value']['file']['file_path'] );
    							}
    							//Delete the file.
    							wp_delete_file( $meta['value']['file']['file_path'] );
    						}
    					}
    				}
    
    				// Add the entry ID into the array.
    				array_push( $deleted_attachments, $result->entry_id );
    				// Clean up the array.
    				$update_del_attachments = array_unique( $deleted_attachments );
    				// Update the option with the cleaned up array.
    				update_option( 'custom_frm_del_att', $update_del_attachments );
    			}
    		}
    	}
    
    	new WPMUDEV_Forminator_Delete_Files_Schedule();
    }

    4] Save and close the file.
    5] The final path should look like /wp-content/mu-plugins/forminator-delete-files-schedule.php

    Needed code edits:
    Find the line that says private static $_form_id = 5; and change number 5 with the ID of the form that you want to setup an event to check & delete it's attachments daily.

    What the code is doing:
    This mu-plugin will setup a "daily" custom scheduled task in your WordPress called "wpmudev_forminator_del_files".

    When this event runs it will check for all submissions that are more than 1 week old of a Form ID that you have given and it. Then it will check if those entries have any uploaded files and delete them. It will be also updating a custom option in the database with the submission IDs that it has already checked just to make it a little bit faster to avoid checking over and over again the same submissions as we can't actually know in a different way that the files where deleted or not.

    Please note that depending on how many entries + attachments your site has this can be an expensive script on execution etc. Also as the form gains more and more entries it will become more expensive as well.

    I've currently set the query to fetch 1 week or older entries just to be sure that everything has been uploaded to your remote system up until then.

    Again, please do make a full backup of the site when adding custom code especially that deals with file deletions etc for safety reasons :slight_smile: .

    --

    Tell me if you need any further help or have any questions about this!

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.