Hello,
I need my web users to send an uploaded file along with their registration data.
The form will be sent via email while at the same time, the uploaded file will be stored in a folder on my server.
Take a look at the code please:

<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "example@noexample.com";
	$email_subject = "Abstract Submission Via Journal Website";
	
	
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
	if(!isset($_POST['first_author_title']) ||
		!isset($_POST['first_author_surname']) ||
		!isset($_POST['first_author_othernames']) ||
		!isset($_POST['second_author_title']) ||
		!isset($_POST['second_author_surname']) ||
		!isset($_POST['second_author_othernames']) ||
		!isset($_POST['third_author_title']) ||
		!isset($_POST['third_author_surname']) ||
		!isset($_POST['third_author_othernames']) ||
		!isset($_POST['presentation_type']) ||
		!isset($_POST['subject_classification']) ||
		!isset($_POST['presentation_title']) ||
		!isset($_POST['first_author_org']) ||
		!isset($_POST['first_author_phone']) ||
		!isset($_POST['first_authoe_email']) ||
		!isset($_POST['second_author_org']) ||
		!isset($_POST['second_author_phone']) ||
		!isset($_POST['second_author_email']) ||
		!isset($_POST['third_author_org']) ||
		!isset($_POST['third_author_phone']) ||
		!isset($_POST['second_author_email']) ||
		!isset($_POST['abstract_uploaded'])) {
		died('We are sorry, you did not fill in required information listed above. Please do so.');		
	}
	
	$first_author_title = $_POST['first_author_title']; // required
	$first_author_surname = $_POST['first_author_surname']; // required
	$first_author_othernames = $_POST['first_author_othernames']; // required
	$second_author_title = $_POST['second_author_title']; // required
	$second_author_surname = $_POST['second_author_surname']; // required
	$second_author_othernames = $_POST['second_author_othernames']; // required
	$third_author_title = $_POST['third_author_title']; // required
	$third_author_surname = $_POST['third_author_surname']; // required
	$third_author_othernames = $_POST['third_author_othernames']; // required
	$presentation_type = $_POST['presentation_type']; // required
	$subject_classification = $_POST['subject_classification']; // required
	$presentation_title = $_POST['presentation_title']; // required
	$first_author_org = $_POST['first_author_org']; // required
	$first_author_phone = $_POST['first_author_phone']; // required
	$first_author_email = $_POST['first_author_email']; // required
	$second_author_org = $_POST['second_author_org']; // required
	$second_author_phone = $_POST['second_author_phone']; // required
	$second_author_email = $_POST['second_author_email']; // required
	$third_author_org = $_POST['third_author_org']; // required
	$third_author_phone = $_POST['third_author_phone']; // required
	$third_author_email = $_POST['third_author_email']; // required
	$abstract_uploaded = $_POST['abstract_uploaded']; // required
		
	$email_message .= "first_author_title: ".clean_string($first_name)."\n";
	$email_message .= "first_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "first_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "second_author_title: ".clean_string($first_name)."\n";
	$email_message .= "second_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "second_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "third_author_title: ".clean_string($first_name)."\n";
	$email_message .= "third_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "third_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "presentation_type: ".clean_string($first_name)."\n";
	$email_message .= "subject_classification: ".clean_string($subject_classification)."\n";
	$email_message .= "presentation_title: ".clean_string($presentation_title)."\n";
	$email_message .= "first_author_org: ".clean_string($first_author_org)."\n";
	$email_message .= "first_author_phone: ".clean_string($first_author_phone)."\n";
	$email_message .= "first_author_email: ".clean_string($first_author_email)."\n";
	$email_message .= "second_author_org: ".clean_string($second_author_org)."\n";
	$email_message .= "second_author_phone: ".clean_string($second_author_phone)."\n";
	$email_message .= "second_author_email: ".clean_string($second_author_email)."\n";
	$email_message .= "third_author_org: ".clean_string($third_author_org)."\n";
	$email_message .= "third_author_phone: ".clean_string($third_author_phone)."\n";
	$email_message .= "third_author_email: ".clean_string($third_author_email)."\n";
	$email_message .= "abstract_uploaded: ".clean_string($abstract_uploaded)."\n";
	
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?
}
?>

<!-- THE PHP SCRIPT I USED FOR PROCESSING THE FILE UPLOAD STARTS HERE -->
<?php 
 $target = "abstract_submitted/"; 
 $target = $target . basename( $_FILES['abstract_uploaded']['name']) ; 
 $ok=1; 
 
 //This is our size condition 
 if ($uploaded_size > 350000) 
 { 
 echo "Your file is too large.<br>"; 
 $ok=0; 
 } 
 
 //This is our limit file type condition 
 if ($uploaded_type =="text/php") 
 { 
 echo "No PHP files<br>"; 
 $ok=0; 
 } 
 
 //Here we check that $ok was not set to 0 by an error 
 if ($ok==0) 
 { 
 Echo "Sorry your file was not uploaded"; 
 } 
 
 //If everything is ok we try to upload it 
 else 
 { 
 if(move_uploaded_file($_FILES['abstract_ uploaded']['tmp_name'], $target)) 
 { 
 echo "The file ". basename( $_FILES['abstract_uploaded']['name']). " has been uploaded"; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 } 
 ?>

A prompt assistance will be much appreciated. Thanks.

Dani AI

Generated

Quick diagnostic (based on ’s posts): the upload part ended up working, so PHP is receiving the file, but the mail step fails because of a few common mistakes in the script: the request-check is looking for the wrong POST field, $email_from and $email_message are never properly set, the code concatenates undefined variables (e.g. \$first_name), some $_FILES values are referenced via undefined names ($uploaded_size, $uploaded_type) and there was a stray space in the move_uploaded_file key. As noted, $email_from must be set; as suggested, stop suppressing errors with @ and enable error reporting to see the real PHP warnings.

Concrete fixes and checklist

  • Make the form method="post" and enctype="multipart/form-data" and ensure the file input name matches (abstract_uploaded).
  • Use a reliable POST test, e.g. if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... } instead of isset($_POST['email']) unless that field actually exists.
  • Initialize $email_message = '' before appending, and use the correct field variables when building the message (not an undefined $first_name). Sanitize inputs to block header injection.
  • For uploads use the $_FILES array correctly: $_FILES['abstract_uploaded']['size'], ['type'], ['tmp_name'], and ['name']; check is_uploaded_file() before move_uploaded_file(). Fix any accidental spaces in array keys.
  • Stop using @mail(...) — check mail()’s boolean return and log failures; many hosts require SMTP authentication, so using a mail library is more reliable.

If attaching the uploaded file to the outgoing email, use a mail library (PHPMailer) rather than crafting MIME by hand. Example (very small, requires PHPMailer installed):

function clean_string($s){ return str_replace(array("\r","\n","%0a","%0d"),' ',trim($s)); }

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->setFrom(clean_string($email_from));
$mail->addAddress($email_to);
$mail->Subject = $email_subject;
$mail->Body = $email_message;
if (!empty($_FILES['abstract_uploaded']['tmp_name']) && is_uploaded_file($_FILES['abstract_uploaded']['tmp_name'])) {
  $mail->addAttachment($_FILES['abstract_uploaded']['tmp_name'], $_FILES['abstract_uploaded']['name']);
}
if (!$mail->send()) { error_log('Mail error: '.$mail->ErrorInfo); }

Additional troubleshooting and hardening

  • Turn on error_reporting(-1); ini_set('display_errors',1); while testing.
  • Test a minimal mail() call to confirm host mail capability; check server mail logs (e.g. exim/postfix logs or hosting control panel).
  • Sanitize filenames, restrict allowed MIME types, limit size, and store uploads outside webroot or rename them to a safe unique name.

Applying the checklist above will make the mail step visible (no silent failures) and, using PHPMailer or SMTP, will reliably deliver the form data together with the file.

Recommended Answers

All 12 Replies

It would be helpful if you stated which error you have, or what part is not working. Just pasting a piece of code won't get you much assistence.

Thanks for the after thought. On pushing the button, the php script file echo "Sorry, there was a problem uploading your file." as indicated on line 140 of the script. Also, no data was delivered to the email account indicated. I hope this sheds some light on the issue.

You did change the $email_to I hope. Also, there is a space on line 134 that should not be there.

I have noticed the space that was not suppose to be there and corrected it however, I seem not to comprehend the $email_to you hinted at. Please a bit more explanation may prove useful.

Line 94 uses $email_to. This variable is set on line 5 to a non existant address. Change that line.

Pritaeas, thanks for your observations. I could get the uploaded files sent to the destination directory successfully however, the form data could not be sent to the genuine email address (not the one I specified in the code I posted). I cold get the uploaded file but the form data was not delivered.
Please advise.

$email_from is never given a value...

I am yet to resolve this issue. Will someone look into why this script would upload file to my server successfully but not send the form data via email.

<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "me@myemailaddress.com";
	$email_subject = "Abstract Submission Via Journal Website";
	
	
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
	if(!isset($_POST['first_author_title']) ||
		!isset($_POST['first_author_surname']) ||
		!isset($_POST['first_author_othernames']) ||
		!isset($_POST['second_author_title']) ||
		!isset($_POST['second_author_surname']) ||
		!isset($_POST['second_author_othernames']) ||
		!isset($_POST['third_author_title']) ||
		!isset($_POST['third_author_surname']) ||
		!isset($_POST['third_author_othernames']) ||
		!isset($_POST['presentation_type']) ||
		!isset($_POST['subject_classification']) ||
		!isset($_POST['presentation_title']) ||
		!isset($_POST['first_author_org']) ||
		!isset($_POST['first_author_phone']) ||
		!isset($_POST['first_authoe_email']) ||
		!isset($_POST['second_author_org']) ||
		!isset($_POST['second_author_phone']) ||
		!isset($_POST['second_author_email']) ||
		!isset($_POST['third_author_org']) ||
		!isset($_POST['third_author_phone']) ||
		!isset($_POST['second_author_email']) ||
		!isset($_POST['abstract_uploaded'])) {
		died('We are sorry, you did not fill in required information listed above. Please do so.');		
	}
	
	$first_author_title = $_POST['first_author_title']; // required
	$first_author_surname = $_POST['first_author_surname']; // required
	$first_author_othernames = $_POST['first_author_othernames']; // required
	$second_author_title = $_POST['second_author_title']; // required
	$second_author_surname = $_POST['second_author_surname']; // required
	$second_author_othernames = $_POST['second_author_othernames']; // required
	$third_author_title = $_POST['third_author_title']; // required
	$third_author_surname = $_POST['third_author_surname']; // required
	$third_author_othernames = $_POST['third_author_othernames']; // required
	$presentation_type = $_POST['presentation_type']; // required
	$subject_classification = $_POST['subject_classification']; // required
	$presentation_title = $_POST['presentation_title']; // required
	$first_author_org = $_POST['first_author_org']; // required
	$first_author_phone = $_POST['first_author_phone']; // required
	$first_author_email = $_POST['first_author_email']; // required
	$second_author_org = $_POST['second_author_org']; // required
	$second_author_phone = $_POST['second_author_phone']; // required
	$second_author_email = $_POST['second_author_email']; // required
	$third_author_org = $_POST['third_author_org']; // required
	$third_author_phone = $_POST['third_author_phone']; // required
	$third_author_email = $_POST['third_author_email']; // required
	$abstract_uploaded = $_POST['abstract_uploaded']; // required
		
	$email_message .= "first_author_title: ".clean_string($first_name)."\n";
	$email_message .= "first_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "first_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "second_author_title: ".clean_string($first_name)."\n";
	$email_message .= "second_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "second_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "third_author_title: ".clean_string($first_name)."\n";
	$email_message .= "third_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "third_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "presentation_type: ".clean_string($first_name)."\n";
	$email_message .= "subject_classification: ".clean_string($subject_classification)."\n";
	$email_message .= "presentation_title: ".clean_string($presentation_title)."\n";
	$email_message .= "first_author_org: ".clean_string($first_author_org)."\n";
	$email_message .= "first_author_phone: ".clean_string($first_author_phone)."\n";
	$email_message .= "first_author_email: ".clean_string($first_author_email)."\n";
	$email_message .= "second_author_org: ".clean_string($second_author_org)."\n";
	$email_message .= "second_author_phone: ".clean_string($second_author_phone)."\n";
	$email_message .= "second_author_email: ".clean_string($second_author_email)."\n";
	$email_message .= "third_author_org: ".clean_string($third_author_org)."\n";
	$email_message .= "third_author_phone: ".clean_string($third_author_phone)."\n";
	$email_message .= "third_author_email: ".clean_string($third_author_email)."\n";
	$email_message .= "abstract_uploaded: ".clean_string($abstract_uploaded)."\n";
	
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

			</br>
			</br>
			</br>
			</br>
			</br>
			</br>
			</br><span class="style19">Your registration was successful. Thank you.
			
			</span>

<?
}
?>
<!-- End of the registration to email script -->

<!-- THE PHP SCRIPT I USED FOR PROCESSING THE FILE UPLOAD STARTS HERE -->
<?php 
 $target = "abstract_submitted/"; 
 $target = $target . basename( $_FILES['abstract_uploaded']['name']) ; 
 $ok=1; 
 
 //This is our size condition 
 if ($uploaded_size > 350000) 
 { 
 echo "Your file is too large.<br>"; 
 $ok=0; 
 } 
 
 //This is our limit file type condition 
 if ($uploaded_type =="text/php") 
 { 
 echo "No PHP files<br>"; 
 $ok=0; 
 } 
 
 //Here we check that $ok was not set to 0 by an error 
 if ($ok==0) 
 { 
 Echo "Sorry your file was not uploaded"; 
 } 
 
 //If everything is ok we try to upload it 
 else 
 { 
 if(move_uploaded_file($_FILES['abstract_uploaded']['tmp_name'], $target)) 
 { 
 echo "The file ". basename( $_FILES['abstract_uploaded']['name']). " has been uploaded"; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 } 
 ?>

What could be wrong? I need you guys' advice.

See previous comment, still unresolved.

Am I right?

<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "emiola@qestinfosystems.com";
	$email_subject = "Abstract Submission Via Journal Website";
	
	
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
	if(!isset($_POST['first_author_title']) ||
		!isset($_POST['first_author_surname']) ||
		!isset($_POST['first_author_othernames']) ||
		!isset($_POST['second_author_title']) ||
		!isset($_POST['second_author_surname']) ||
		!isset($_POST['second_author_othernames']) ||
		!isset($_POST['third_author_title']) ||
		!isset($_POST['third_author_surname']) ||
		!isset($_POST['third_author_othernames']) ||
		!isset($_POST['presentation_type']) ||
		!isset($_POST['subject_classification']) ||
		!isset($_POST['presentation_title']) ||
		!isset($_POST['first_author_org']) ||
		!isset($_POST['first_author_phone']) ||
		!isset($_POST['first_authoe_email']) ||
		!isset($_POST['second_author_org']) ||
		!isset($_POST['second_author_phone']) ||
		!isset($_POST['second_author_email']) ||
		!isset($_POST['third_author_org']) ||
		!isset($_POST['third_author_phone']) ||
		!isset($_POST['second_author_email']) ||
		!isset($_POST['abstract_uploaded'])) {
		died('We are sorry, you did not fill in required information listed above. Please do so.');		
	}
	
	$first_author_title = $_POST['first_author_title']; // required
	$first_author_surname = $_POST['first_author_surname']; // required
	$first_author_othernames = $_POST['first_author_othernames']; // required
	$second_author_title = $_POST['second_author_title']; // required
	$second_author_surname = $_POST['second_author_surname']; // required
	$second_author_othernames = $_POST['second_author_othernames']; // required
	$third_author_title = $_POST['third_author_title']; // required
	$third_author_surname = $_POST['third_author_surname']; // required
	$third_author_othernames = $_POST['third_author_othernames']; // required
	$presentation_type = $_POST['presentation_type']; // required
	$subject_classification = $_POST['subject_classification']; // required
	$presentation_title = $_POST['presentation_title']; // required
	$first_author_org = $_POST['first_author_org']; // required
	$first_author_phone = $_POST['first_author_phone']; // required
	$first_author_email = $_POST['first_author_email']; // required
	$second_author_org = $_POST['second_author_org']; // required
	$second_author_phone = $_POST['second_author_phone']; // required
	$second_author_email = $_POST['second_author_email']; // required
	$third_author_org = $_POST['third_author_org']; // required
	$third_author_phone = $_POST['third_author_phone']; // required
	$third_author_email = $_POST['third_author_email']; // required
	$abstract_uploaded = $_POST['abstract_uploaded']; // required
	$email_from = $_POST['first_author_email']; // required


	
	$email_message .= "first_author_title: ".clean_string($first_name)."\n";
	$email_message .= "first_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "first_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "second_author_title: ".clean_string($first_name)."\n";
	$email_message .= "second_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "second_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "third_author_title: ".clean_string($first_name)."\n";
	$email_message .= "third_author_surname: ".clean_string($first_name)."\n";
	$email_message .= "third_author_othernames: ".clean_string($first_name)."\n";
	$email_message .= "presentation_type: ".clean_string($first_name)."\n";
	$email_message .= "subject_classification: ".clean_string($subject_classification)."\n";
	$email_message .= "presentation_title: ".clean_string($presentation_title)."\n";
	$email_message .= "first_author_org: ".clean_string($first_author_org)."\n";
	$email_message .= "first_author_phone: ".clean_string($first_author_phone)."\n";
	$email_message .= "first_author_email: ".clean_string($first_author_email)."\n";
	$email_message .= "second_author_org: ".clean_string($second_author_org)."\n";
	$email_message .= "second_author_phone: ".clean_string($second_author_phone)."\n";
	$email_message .= "second_author_email: ".clean_string($second_author_email)."\n";
	$email_message .= "third_author_org: ".clean_string($third_author_org)."\n";
	$email_message .= "third_author_phone: ".clean_string($third_author_phone)."\n";
	$email_message .= "third_author_email: ".clean_string($third_author_email)."\n";
	$email_message .= "abstract_uploaded: ".clean_string($abstract_uploaded)."\n";
	
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

			</br>
			</br>
			</br>
			</br>
			</br>
			</br>
			</br><span class="style19">Your registration was successful. Thank you.
			
			</span>

<?
}
?>
<!-- End of the registration to email script -->

<!-- THE PHP SCRIPT I USED FOR PROCESSING THE FILE UPLOAD STARTS HERE -->
<?php 
 $target = "abstract_submitted/"; 
 $target = $target . basename( $_FILES['abstract_uploaded']['name']) ; 
 $ok=1; 
 
 //This is our size condition 
 if ($uploaded_size > 350000) 
 { 
 echo "Your file is too large.<br>"; 
 $ok=0; 
 } 
 
 //This is our limit file type condition 
 if ($uploaded_type =="text/php") 
 { 
 echo "No PHP files<br>"; 
 $ok=0; 
 } 
 
 //Here we check that $ok was not set to 0 by an error 
 if ($ok==0) 
 { 
 Echo "Sorry your file was not uploaded"; 
 } 
 
 //If everything is ok we try to upload it 
 else 
 { 
 if(move_uploaded_file($_FILES['abstract_uploaded']['tmp_name'], $target)) 
 { 
 echo "The file ". basename( $_FILES['abstract_uploaded']['name']). " has been uploaded"; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 } 
 ?>

For Pritaeas

At least now it is set.

Remove the error suppression (@) from the mail function on line 98. After this, run a test and see if you get any errors.

If the above didn't help, try putting this at the top of your code:

error_reporting(-1);
ini_set('display_errors','On');

and let us know what comes up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.