Hi, i'm new about PHP, and i try to insert the PHP Upload script in the php mail, but somehow i don't get it to work! :)

this actually goes to header:

<?php
  if(isset($_POST['submit'])) {
	  
      $errors = array();

      $allowed_filetypes = array('.pdf','.zip','.rar'); 
      $max_filesize = 52428800; 
      $upload_path = './uploads_SS/'; 
 
   $filename = $_FILES['userfile']['name']; 
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
 
   if(!in_array($ext,$allowed_filetypes))
      $errors[] = "The file you attempted to upload is not allowed.";
 
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      $errors[] = "The file you attempted to upload is too large.";
 
   if(!is_writable($upload_path))
      $errors[] = "You cannot upload to the specified directory, please CHMOD it to 777.";
 
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; 
      else
         echo 'There was an error during the file upload.  Please try again.'; 
 
      if($_POST['fname'] == "") {
         $errors[] = "The First Name field is empty";
      }
 
      if(count($errors) == 0) {
         $sendto = "someone@email.com";
         $title = "Application Form Submit";
         $fname = $_POST['fname'];
         $lname = $_POST['lname'];
         $Email = $_POST['Email'];
         $title_s = $_POST['title_s'];
		 $dob = $_POST['dob'];
         $mob = $_POST['mob'];
		 $yob = $_POST['yob'];
		 $country = $_POST['country'];
		 $saddress = $_POST['saddress'];
		 $city = $_POST['city'];
		 $zip = $_POST['zip'];
		 $phone = $_POST['phone'];
		 $fax = $_POST['fax'];
		 $userfile = $_POST['userfile'];
		 $adinfo = $_POST['adinfo'];
$message = <<<DATA
Name: $title_s $fname $lname
Email: $Email
Date of Birth: $dob, $mob, $yob
Country Territory: $country
Street Address: $saddress
City/Town: $city
Postal Code: $zip
Phone Number: $phone
Fax Number: $fax 
Attachments: $userfile
Additional information: $userfile
DATA;
         if(mail($sendto, $title, $message)) {
             $success = true;
         } else {
            $success = false;
         }
    } else {
       $success = false;

    }
  }

?>

and this in <body>

<?php
  if(isset($_POST['submit'])) {
     if($success == true && count($errors) == 0) {
        echo "&nbsp;&nbsp;&nbsp;&nbsp;<font color='#FF0000'>Your Submission has been Completed.</font>";
     }
     if(count($errors) == 0 && $success == false && isset($_POST['submit'])) {
        echo '&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000">There was a problem with our form. Please email us directly via [email]someone@email.com[/email], thank you.</font>';
     }

     if($success == false && count($errors) > 0 && isset($_POST['submit'])) {
        echo "<ul>";
        foreach($errors as $e) {
           echo "<font color='#FF0000'><li>$e</li></font>";
        }
        echo "</ul>";
     }
 }

 ?>

I know the script above isn't clean!! :D
but I'm new on it :-O
thank you very much for your help!

Dani AI

Generated

you are close. The two blockers are (a) mixing up file vs form data and (b) not actually attaching the upload to the email. Use $_FILES['userfile'] (not $_POST) and make sure the HTML form uses enctype="multipart/form-data". Then check $_FILES['userfile']['error'] and proceed only when it equals UPLOAD_ERR_OK. To enforce allowed types (your .pdf/.zip/.rar), whitelist extensions and also verify the MIME type with Fileinfo; do not trust the browser’s reported type. Store uploads outside the web root and give them a random filename before you attach them to the email. Using PHPMailer is much simpler than crafting MIME with mail(). (php.net)

Here is a compact pattern you can drop in. It validates extension, MIME, size (50 MB), safely stores the file, and emails it as an attachment:

<?php
// form must be POST with enctype="multipart/form-data" and input name="userfile"
if (!isset($_FILES['userfile']) || $_FILES['userfile']['error'] !== UPLOAD_ERR_OK) exit('Upload error');

$ext  = strtolower(pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION));
$mime = (new finfo(FILEINFO_MIME_TYPE))->file($_FILES['userfile']['tmp_name']);

if (!in_array($ext, ['pdf','zip','rar'], true)) exit('Bad extension');
if (!in_array($mime, ['application/pdf','application/zip','application/x-rar-compressed','application/x-rar'], true)) exit('Bad type');
if ($_FILES['userfile']['size'] > 50*1024*1024) exit('Too big');

$dir = __DIR__ . '/uploads'; if (!is_dir($dir)) mkdir($dir, 0750, true);
$dest = $dir . '/' . bin2hex(random_bytes(16)) . '.' . $ext;
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $dest)) exit('Move failed');

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->setFrom('no-reply@example.com');
$mail->addAddress('someone@email.com');
$mail->Subject = 'Application Form Submit';
$mail->Body = "Name: {$_POST['title_s']} {$_POST['fname']} {$_POST['lname']}";
$mail->addAttachment($dest, $_FILES['userfile']['name']);
$mail->send();

Quick checks that often trip people up (echoing ’s point about moving the file): ensure upload_max_filesize and post_max_size in php.ini allow your target sizes (and keep post_max_size larger). (php.net)

This works for me:

//File upload

// Where the file is going to be placed 
$target_path = "uploads/";

// Add the original filename to our target path.  
//Result is "uploads/filename.extension" 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}	

//End of file upload

'uploadedfile' is the name of the 'file' input type element in the HTML form.

Hi levsh!! thanks!!, but is there any option available for only $allowed_filetypes?
thank you very much!

Hi levsh!! thanks!!, but is there any option available for only $allowed_filetypes?
thank you very much!

I'm sure there is. I just haven't gotten to that point myself.
I'm having trouble attaching an uploaded file to an email. When I get this to working, I'll worry about the allowed filetypes. :)

i changed you body code replace your 'body' code with this and let me knw results ->

<?php

if(isset($_POST)){
if($success==true&&count($errors)==0){
echo"&nbsp;&nbsp;&nbsp;&nbsp;<fontcolor='#FF0000'>YourSubmissionhasbeenCompleted.</font>";

}

if(count($errors)==0&&$success==false&&isset($_POST)){

echo'&nbsp;&nbsp;&nbsp;&nbsp;<fontcolor="#FF0000">Therewasaproblemwithourform.Pleaseemailusdirectlyvia<ahref="mailto:"></a>,thankyou.</font>';

}

if($success==false&&count($errors)>0&&isset($_POST)){

echo"<ul>";

//foreach($errorsas$e){

echo"<fontcolor='#FF0000'><li>$e</li></font>";

//}

echo"</ul>";

}

}

?>

thank you my frind .................

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.