Contact form, attachment file size

3 replies · opened Dec 29, 2016

GgmohdezbDec 29, 2016

Hello,

How can I limit the file size of an attachment for the contact form? I didn't found any similar question or related content in the documentation, can you please tell me how? thank you :)

Happy new year by the way :)

GgmohdezbDec 30, 2016

And also the file type please :)

SSemicolon WebSTAFFJan 4, 2017

Hello,

A Very Happy New Year to you too! :)

This is Definitely Possible. Please find the following code in the include/sendemail.php File:

// Runs only when File Field is present in the Contact Form
if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {
	$mail->IsHTML(true);
	$mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
}

and replace it with:

// Runs only when File Field is present in the Contact Form
if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {

	$allowed_files = array('application/doc', 'application/pdf', 'another/type');

	if( !in_array( $_FILES['template-contactform-file']['type'], $allowed_files ) ) {
		echo '{ "alert": "error", "message": "File type not allowed! Please Try Again." }';
		die;
	}

	if( $_FILES["template-contactform-file"]["size"] > 4000000 ) { // 4MB
		echo '{ "alert": "error", "message": "File Size exceeds the Limit! Please Try Again." }';
		die;
	}

	$mail->IsHTML(true);
	$mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
}

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

GgmohdezbJan 13, 2017

Thank you very much!, it is working nice! thank you!

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard