Hi, I've made a form and it can send an e-mail with the text the person filled out.

But now I'm trying to do so the person can upload a file and that will be included in the e-mail as an attachment.

I think the problem is that I don't refer anywhere to the upload formtag named "upfile"
how would I do that?

And do you think that is the problem?
Because it sends the text fields in the e-mail it's just missing the attachment?

This is my code:

<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
	
	//Get the uploaded file information
	$name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);
	//get the file extension of the file
	$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);
	
	$size_of_uploaded_file = 
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs
	
	//Settings 
	$max_allowed_file_size = 100; // size in KB 
	$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");

	//Validations
	if($size_of_uploaded_file > $max_allowed_file_size ) 
{
  	$errors .= "\n Size of file should be less than $max_allowed_file_size";
}

	//------ Validate the file extension -----
	$allowed_ext = false;
	for($i=0; $i<sizeof($allowed_extensions); $i++) 
{ 
  	if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
  {
    $allowed_ext = true;    
  }
}

	if(!$allowed_ext)
{
 	 $errors .= "\n The uploaded file is not supported file type. ".
  	" Only the following file types are supported: ".implode(',',$allowed_extensions);
}

	//copy the temp. uploaded file to uploads folder
	$path_of_uploaded_file = $images . $name_of_uploaded_file;
	$tmp_path = $_FILES["uploaded_file"]["tmp_name"];

	if(is_uploaded_file($tmp_path))
{
  	if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
}



	
    mail("my@e-mail.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form method='post' action='formmail.php' ENCTYPE='multipart/form-data'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  File to upload:<br>
  <INPUT TYPE='FILE' NAME='upfile'><br>
  <input type='submit' />
  </form>";
  }
?>

Thanks

Hi again,

now i've gotten abit further.
Now at least the file uploaded is put into a folder on the server named "uploads"
But is there a way I can get that file to be included in the e-mail as an attachment?

This is my code now:

<?php




function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
   

ini_set('error_reporting', E_ALL);

$dist = $_SERVER["DOCUMENT_ROOT"]."/sendmail/formmail_with_attachment/uploads";

    // Er upload tilladt ???
    if(false == ini_get('file_uploads')) {
        echo "Upload af filer er ikke tilladt i php.ini!";
        exit;
    }

    // Findes dir til upload ???
    if(!is_dir($dist)) {
        echo "Dir til upload findes ikke!";
        exit;
    }

    if(!is_writable($dist)) {
        echo "Der er ikke skriverettigheder på uploaddir, ". $dist;
        exit;
    }



if(isset($_FILES["upfile"]) && is_uploaded_file($_FILES["upfile"]["tmp_name"]))
{
$filnavn = $_FILES["upfile"]["name"];
$tempnavn = $_FILES["upfile"]["tmp_name"];

    if(move_uploaded_file($tempnavn, $dist."/".$filnavn)) {
    chmod($dist."/".$filnavn, 0644);
    echo"Din fil er uploadet!";

    }
    else
    {

    echo"Filen kunne ikke uploades";

    }
   
}
else
    {
    echo"filen er der ikke";
    }

    mail("minmail@hotmail.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }

?>

Hope someone can help?

Thank you ;)

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.