Skip to content

Forminator PRO: External SFTP uplaod for attachments and form uploads

I know that there are snippets to store form uploads in different folders, but I would like to upload them to an external FTP server.

Could you please guide me in the right direction, how to get the temporary uploaded file to use it for FTP-upload instead of storing it in the regular folder? 

We have worked on this request and prepared an example snippet to upload files to an external server. Just add a must-use plugin to your site’s wp-content/mu-plugins folder or add this code to the themes functions.php file.

add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
	if( $form_id != 361 ) { //Please change the form ID
		return $submit_errors;
	}

	if( empty( $submit_errors ) ){
		foreach( $field_data_array as $key => $value ){
			if( $value['name'] == 'upload-1' ){
				if( is_array( $value['value']['file'] ) ){
					if( $value['value']['file']['file_url'] != '' ){
						$file_url = $value['value']['file']['file_url'];
						$ftp_hostname = 'ftp.example.com'; // please change this
						$ftp_username = 'ftpusername'; // please change this
						$ftp_password = 'ftppassword'; // please change this
						$remote_dir = '/path/to/folder'; // please change this
						$src_file = basename($file_url);

						//upload file
						if ($src_file!=''){
							// remote file path
							$dst_file = $remote_dir . $src_file;
							
							// connect ftp
							$ftpcon = ftp_connect($ftp_hostname) or die('Error connecting to ftp server.');
							
							// ftp login
							$ftplogin = ftp_login($ftpcon, $ftp_username, $ftp_password);
							
							// ftp upload
							if (ftp_put($ftpcon, $dst_file, $file_url, FTP_ASCII))
								return $submit_errors;
							else
							$submit_errors[]['submit'] = __('File not uploaded.', 'forminator');
							
							// close ftp stream
							ftp_close($ftpcon);
						}
					}
				}
			}
		}
	}

	return $submit_errors;
},15,3);

Note: This code is just an example snippet, before testing please change the form ID from 361 to your form’s ID and we are assuming upload field ID as upload-1 please change that too if it’s different. Also, please change the details here

$ftp_hostname = 'ftp.example.com'; // please change this
$ftp_username = 'ftpusername'; // please change this
$ftp_password = 'ftppassword'; // please change this
$remote_dir = '/path/to/folder'; // please change this

We recommend to test this on the dev/staging version first before putting it on the live site.

Read more: https://wpmudev.com/forums/topic/forminator-pro-forminator-external-ftp-upload-of-form-uploads/

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen