Multiple file upload not working

3 replies · opened Oct 13, 2016

Mmscoates29Oct 13, 2016

Hi, I managed to solve my other problem with the form returning, however I have one last problem I need help with (hopefully very small). I need my form to support multiple file uploads, and although I had it working fine with the single file uploader, now my files aren't attached when my form returns. I have appended my file name with [] as stated in the documentation for the upload file widget but I'm still having no luck - please help!

My form is live at www.brief-tro.com

The form returns everything fine except the files aren't attached. Thanks in advance!

Mmscoates29Oct 13, 2016

Sorry, the php code for sendmail.php is:

`<?php

require_once('phpmailer/PHPMailerAutoload.php');

$toemails = array();

$toemails[] = array(
'email' => 'xxxx', // Your Email Address
'name' => 'xxxx' // Your Name
);

// Form Processing Messages
$message_success = 'Your brief has been **successfully ** received and will now be processed. Thank you.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtpout.europe.secureserver.net";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Host = "smtpout.europe.secureserver.net";
$mail->Port = 80;
$mail->Username = "xxxx";
$mail->Password = "xxxx";

// If you intend you use SMTP, add your SMTP Code after this Line

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( $_POST['contactform-jobnumber'] != '' ) {

	$jobnumber = isset( $_POST['contactform-jobnumber'] ) ? $_POST['contactform-jobnumber'] : '';
	$client = isset( $_POST['contactform-client'] ) ? $_POST['contactform-client'] : '';
	$projecttitle = isset( $_POST['contactform-projecttitle'] ) ? $_POST['contactform-projecttitle'] : '';
	$requestedby = isset( $_POST['contactform-requestedby'] ) ? $_POST['contactform-requestedby'] : '';
	$reviewdate = isset( $_POST['contactform-reviewdate'] ) ? $_POST['contactform-reviewdate'] : '';
	$finaldeadline = isset( $_POST['contactform-finaldeadline'] ) ? $_POST['contactform-finaldeadline'] : '';
	$maxtimeallowed = isset( $_POST['contactform-maxtimeallowed'] ) ? $_POST['contactform-maxtimeallowed'] : '';
	$assetsorlead = isset( $_POST['contactform-assetsorlead'] ) ? $_POST['contactform-assetsorlead'] : '';
	$projecttype = isset( $_POST['contactform-projecttype'] ) ? $_POST['contactform-projecttype'] : '';
	$serverlocation = isset( $_POST['contactform-serverlocation'] ) ? $_POST['contactform-serverlocation'] : '';
	$clientoverview = isset( $_POST['contactform-clientoverview'] ) ? $_POST['contactform-clientoverview'] : '';
	$othercomments = isset( $_POST['contactform-othercomments'] ) ? $_POST['contactform-othercomments'] : '';

	$subject = isset($subject) ? $subject : 'New Creative Brief Received';

	$botcheck = $_POST['template-contactform-botcheck'];

	if( $botcheck == '' ) {

		$mail-&gt;SetFrom( $email , $name );
		$mail-&gt;AddReplyTo( $email , $name );
		foreach( $toemails as $toemail ) {
			$mail-&gt;AddAddress( $toemail['email'] , $toemail['name'] );
		}
		$mail-&gt;Subject = $subject;

		$jobnumber = isset($jobnumber) ? "Job Number: $jobnumber&lt;br&gt;&lt;br&gt;" : '';
		$client = isset($client) ? "Client: $client&lt;br&gt;&lt;br&gt;" : '';
		$projecttitle = isset($projecttitle) ? "Project Title: $projecttitle&lt;br&gt;&lt;br&gt;" : '';
		$requestedby = isset($requestedby) ? "Requested By: $requestedby&lt;br&gt;&lt;br&gt;" : '';
		$projecttype = isset($projecttype) ? "Project Type: $projecttype&lt;br&gt;&lt;br&gt;" : '';
		$reviewdate = isset($reviewdate) ? "Review Date: $reviewdate&lt;br&gt;&lt;br&gt;" : '';
		$finaldeadline = isset($finaldeadline) ? "Final Deadline: $finaldeadline&lt;br&gt;&lt;br&gt;" : '';
		$maxtimeallowed = isset($maxtimeallowed) ? "Maximum Time Allowed (Hours): $maxtimeallowed&lt;br&gt;&lt;br&gt;" : '';
		$assetsorlead = isset($assetsorlead) ? "Existing Assets or Creative Lead: $assetsorlead&lt;br&gt;&lt;br&gt;" : '';
		$serverlocation = isset($serverlocation) ? "Location on Server of files: $serverlocation&lt;br&gt;&lt;br&gt;" : '';
		$clientoverview = isset($clientoverview) ? "Client Overview: $clientoverview&lt;br&gt;&lt;br&gt;" : '';
		$othercomments = isset($othercomments) ? "Other Comments: $othercomments&lt;br&gt;&lt;br&gt;" : '';
	

		$referrer = $_SERVER['HTTP_REFERER'] ? '&lt;br&gt;&lt;br&gt;&lt;br&gt;This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

		$body = "$jobnumber $client $projecttitle $requestedby $projecttype $reviewdate $finaldeadline $maxtimeallowed $assetsorlead $serverlocation $othercomments ";

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

if ( isset( $_FILES['contactform-file'] ) && $_FILES['contactform-file2']['error'] == UPLOAD_ERR_OK ) {
$mail->IsHTML(true);
$mail->AddAttachment( $_FILES['contactform-file2']['tmp_name'], $_FILES['contactform-file2']['name'] );
}

		// Runs only when reCaptcha is present in the Contact Form
		if( isset( $_POST['g-recaptcha-response'] ) ) {
			$recaptcha_response = $_POST['g-recaptcha-response'];
			$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );

			$g_response = json_decode( $response );

			if ( $g_response-&gt;success !== true ) {
				echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
				die;
			}
		}

		$mail-&gt;MsgHTML( $body );
		$sendEmail = $mail-&gt;Send();

		if( $sendEmail == true ):
			echo '{ "alert": "success", "message": "' . $message_success . '" }';
		else:
			echo '{ "alert": "error", "message": "Email **could not** be sent due to some Unexpected Error. Please Try Again later.&lt;br /&gt;&lt;br /&gt;**Reason:**&lt;br /&gt;' . $mail-&gt;ErrorInfo . '" }';
		endif;
	} else {
		echo '{ "alert": "error", "message": "Bot **Detected**.! Clean yourself Botster.!" }';
	}
} else {
	echo '{ "alert": "error", "message": "Please **Fill up** all the Fields and Try Again." }';
}

} else {
echo '{ "alert": "error", "message": "An unexpected error occured. Please Try Again later." }';
}

?>

Mmscoates29Oct 14, 2016

Hi, my problem has still not bee solved, are you able to help?

Thanks

SSemicolon WebSTAFFOct 17, 2016

Hello,

[sb_private_reply]We Really apologize about the Delays in replying to your Support Request![/sb_private_reply]

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

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:

if( isset( $_FILES['template-contactform-file'] ) AND is_array( $_FILES['template-contactform-file'] ) ) {
	$countfiles = count( $_FILES['template-contactform-file'] );
	for( $i=0; $i IsHTML(true);
			$mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'][$i], $_FILES['template-contactform-file']['name'][$i] );
		}
	}
}

This will definitely work fine. Also, do make sure that you have changed the Form's File Input's name Attribute from template-contactform-file to template-contactform-file[] as this is important to include Multiple Files in the Form. Thanks for your Patience.

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

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