PHP发送带有多个文件的电子邮件表格上传到服务器

时间:2018-09-29 21:45:59

标签: php html html-email html-form html-form-post

我无法正确使用脚本。

我从包含在<input>下面的表单中获取文件上传的输入:

<form class="contactform" action="/php/mail.php" method="POST" enctype="multipart/form-data"> 
<input type="file" id="file-upload" name="uploaded_file[]" multiple="multiple" accept="image/*" />
</form>

多个文件应存储在数组中,以上传到创建的文件夹中。这些文件应在处理之前经过验证,但是如果上传了任何文件,则应首先进行验证。如果文件已上传,则应处理一个电子邮件表格,其中包括文件名和其他信息。如果没有,则应处理其他电子邮件表单。

创建了一个小会话来处理一些信息以“确认”页面。

下面的脚本似乎只处理以后的电子邮件表单,而不管我做什么。

我正在使用PHP 5.6。

<?php
session_start();

//Settings
$max_allowed_file_size = 2048; // size in KB

//$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf", "png");
error_reporting(E_ALL);

$modtager = "simon@stutteri-volstrup.dk";
$recipient = $modtager;

//Get values from from
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$company = $_POST['company'];
$type = $_POST['type'];
$adress = $_POST['adress'];
$hesteid = $_POST['hesteid'];
$hestenavn = $_POST['hestenavn'];
$message = $_POST['message'];
$areacode = $_POST['areacode'];
$land = $_POST['land'];
$name = $_POST['name'];

//Session variables
$_SESSION["sname"] = $name;
$_SESSION["semail"] = $email;
$_SESSION["sphone"] = $phone;


if (empty($_FILES['uploaded_file']['tmp_name'])) 
{
    //Upload folder
    //name for mail

    $d = date('y.m.d');
    $varfoldername = $_SERVER['DOCUMENT_ROOT']."/receivedfiles/" . $d . " " . $name . "/";
    $varemailfoldername ="/" . $d ." " . $name . "/";
    if(!is_dir($varfoldername)) 
    {
        mkdir($varfoldername , 0777 , true);
    }
    $upload_folder = $varfoldername;
    $emailupload_folder = $varemailfoldername;

    //Uploaded file
    // Loop through each file
    for( $i=0 ; $i < $total ; $i++ )
    {
        //Get the uploaded file information

        $name_of_uploaded_file = basename($_FILES["uploaded_file"]["name"][$i]);

        //Validations

        //Validate size requirements
        $size_of_uploaded_file = $_FILES["uploaded_file"]["size"][$i]/1024;//size in KBs

        if($size_of_uploaded_file > $max_allowed_file_size)
        {
            die("Fejl: Filen er for stor");
        }
        else
        {
            $sizecheck = true;
        }

        //------ Validate the file extension -----
        //get the file extension of the file
        $type_of_uploaded_file =
        substr($name_of_uploaded_file,
        strrpos($name_of_uploaded_file, '.') + 1);
        $allowed_ext = false;
        for($i=0; $i<sizeof($allowed_extensions); $i++)
        {
            if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
            {
                $allowed_ext = true;
            }
            else 
            {
                $ext_check = true;
            }
        }

        if(!$allowed_ext)
        {
            die("The uploaded file is not supported file type. \n Send venligst filer af følgende type: jpg, jpeg, gif, bmp, pdf");
        }

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

        if(is_uploaded_file($tmp_path))
        {
            if($sizecheck || $ext_check)
            {
                move_uploaded_file ( $tmp_path,$path_of_uploaded_file );
            }
        }

    else
    {
        die("Error while copying the uploaded file");}
    }

    //Send email
    $formcontent=" Fra: $email \n Firma: $company \n Navn: $name \n Adresse: $adress , $areacode \n Land: $land \n Telefon: $phone \n Ringes op: $call \n Type: $type \n Hoppens navn og ID: $hestenavn , $hesteid \n Besked: \n $message \n \n Vedhæftede filer: \n array($emailpath_of_uploaded_file)";
    $subject = "Besked fra hjemmesiden fra $name";
    $mailheader = "Fra: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header('Location: https://www.homepage.com');
}
else 
{
    $emailpath_of_uploaded_file = "Ingen";
    //Send email
    $formcontent=" Fra: $email \n Firma: $company \n Navn: $name \n Adresse: $adress , $areacode \n Land: $land \n Telefon: $phone \n Ringes op: $call \n Type: $type \n Hoppens navn og ID: $hestenavn , $hesteid \n Besked: \n $message \n \n Vedhæftede filer: \n $emailpath_of_uploaded_file";
    $subject = "Besked fra hjemmesiden fra $name";
    $mailheader = "Fra: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header('Location: https://www.homepage.com');
}
?>

0 个答案:

没有答案