0

Thanks in advance for your answers. I am looking to add a file upload feature to my PHP contact form script. So a user is able to upload a CV for example.

The PHP script below works very well and helps to reduce spam as well as being really easy to understand. So we would like to keep this format and not complicate it much more.

How would I go about editing the existing script to include this function?

Any help appreciated.


/**
*
* Function to validate an email address
*
**/

function validate_email($email)
{
    //Perform a basic syntax-Check
    //If this check fails, there's no need to continue
    $email = filter_var($email, FILTER_SANITIZE_STRING);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL))
        return false;

    //extract host
    list($user, $host) = explode("@", $email);
    //check, if host is accessible
    if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
        return false;
        
    return true;
}

/**
*
* Get/set variables
*
**/

$name       = isset($_POST['name'])     ? htmlspecialchars(stripslashes($_POST['name']), ENT_QUOTES, "UTF-8")       : '';
$email      = isset($_POST['email'])    ? htmlspecialchars(stripslashes($_POST['email']), ENT_QUOTES, "UTF-8")      : '';
$confirm    = isset($_POST['confirm'])  ? htmlspecialchars(stripslashes($_POST['confirm']), ENT_QUOTES, "UTF-8")    : '';
$message    = isset($_POST['message'])  ? htmlspecialchars(stripslashes($_POST['message']), ENT_QUOTES, "UTF-8")    : '';
$SPAM       = $_POST["spam-check"];

/**
*
* Check no empty values
*
**/

if (empty($name) OR empty($email) OR empty($confirm) OR empty($message) OR !empty($SPAM) OR !validate_email($email) OR ($email !== $confirm))
{
    exit("There was an error whilst processing your request, please call us or try again later – Thank You");
}
else
{   
    $subject = "Website Contact Form Submit";
    $to      = "email@myemail.co.uk";

    $body = "Name: " . $name . "\n";
    $body .= "Email: " . $email . "\n\n";
    $body .= "Message:\n" . $message;

    $header = "From: " . $name . " <" . $email . ">\n";
    $header .= "Reply-To: " . $name . " <" . $email . ">\n";

    if ( @mail($to, $subject, $body, $header) )
    {
        echo json_encode( array("sent"=>"success", "feedback"=>"Your query has been sent successfully") );
        exit;
    }
}```

  • Do you want to upload a file or send it through mail? – Smurker Jul 30 '21 at 08:39
  • @Smurker I want the user to have the option to upload a file on the contact form. Which is then sent via email like the rest of the contact form. I hope this is clear. – Nathan Clancy Jul 30 '21 at 08:53
  • You problem is then split into two parts. [Upload a file to a remote server](https://stackoverflow.com/a/35253947/3007852) and then [send it via email](https://stackoverflow.com/a/12302354/3007852). Take a look at the answers i linked you to and give them a try! – Smurker Jul 30 '21 at 09:01

1 Answers1

0

For example you have input for file/cv upload like this:

<input name="userfile" type="file" />
  1. Create new function in your script
function upload_file($file_input, $upload_dir = 'uploads/')
  {
  // return false if error occurred
 $error = $_FILES[$file_input]['error'];
   
        if ($error !== UPLOAD_ERR_OK) {
            return false;
        }
    
        // move the uploaded file to the upload_dir
        $new_file = $upload_dir . $_FILES[$file_input]['name'];
    
        move_uploaded_file($_FILES[$file_input]['tmp_name'], $new_file);
    }}

And just pass file in your validate_email() function like this:

/**
*
* Get/set variables
*
**/

$name       = isset($_POST['name'])     ? htmlspecialchars(stripslashes($_POST['name']), ENT_QUOTES, "UTF-8")       : '';
$email      = isset($_POST['email'])    ? htmlspecialchars(stripslashes($_POST['email']), ENT_QUOTES, "UTF-8")      : '';
$confirm    = isset($_POST['confirm'])  ? htmlspecialchars(stripslashes($_POST['confirm']), ENT_QUOTES, "UTF-8")    : '';
$message    = isset($_POST['message'])  ? htmlspecialchars(stripslashes($_POST['message']), ENT_QUOTES, "UTF-8")    : '';
$SPAM       = $_POST["spam-check"];
// Our file from request
$file_input = $_FILES["userfile"];

// upload if exists
if(isset($file_input)
{
    upload_file($file_input);
}