mardi 8 septembre 2015

PHP if statement placement

i have a listener for an ajax apply form. The form has the option to upload a file.

At the moment it will only work if a file is included. I can't seem to get the if statements correct.

if( isset($_POST['type']) && $_POST['type'] == 'submit' ){

if( isset($_POST['formname']) && $_POST['formname'] == 'contact'){


    if (isset($_FILES["file"])) {

        $error = "";
        $allowedExts = array("doc", "docx", "pdf", "odt");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);


if ($_FILES["file"]["error"] > 0) {
    $error .= "Error opening the file<br />";
}

if (!in_array($extension, $allowedExts)) {
    $error .= "File type not allowed, please try again<br />";
}

if ($_FILES["file"]["size"] > 102400) {
    $error .= "File size shoud be less than 100 kB<br />";
}



     if ($error == "") {



        $to = "email@mail.com";



        $attachment = (WP_CONTENT_DIR.'/uploads/'.basename($_FILES['file']['name']));
        move_uploaded_file($_FILES["file"]["tmp_name"], $attachment);


        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];


        $body = "From: $name\n E-Mail: $email\n Message: $message\n";


        $headers = "From: " . $name . "<". $email .">\r\n";

        wp_mail( $to, 'Enquiry', $body, $headers, array($attachment));

        unlink($attachment);

        echo 'Thankyou for your enquiry.';
        exit;


}else{

    echo $error;
    exit;
   }
  }      
 } 
}

So if a file is not set then i want the script to continue and send anyway..

Aucun commentaire:

Enregistrer un commentaire