简体   繁体   English

通过HTML5和PHP中的联系表单发送文件

[英]Sending A File Via A Contact Form In HTML5 And PHP

before I ask my question please allow me to say that I am not a php developer currently. 在我问我的问题之前,请允许我说我当前不是PHP开发人员。 I have a little experience with PHP but my background is mainly with HTML5, CSS3, SASS, JS, Jquery, Basic PHP. 我对PHP有一点经验,但是我的背景主要是HTML5,CSS3,SASS,JS,Jquery和Basic PHP。

With that in mind here is my problem. 考虑到这一点,这是我的问题。 Currently I have a html5 based form which I am using to parse a name and recipient email field + attach a document. 目前,我有一个基于html5的表单,用于解析名称和收件人电子邮件字段+附加文档。

I have the email sending and the document, however regardless of which doc type I send it comes through as a txt file and the file itself is always empty. 我有电子邮件发送和文档,但是无论我发送哪种文档类型,它都是作为txt文件发送的,并且文件本身始终为空。 So my question is why? 所以我的问题是为什么? / What Am I Doing wrong? / 我究竟做错了什么?

I know I need to do other things to the form ie validation, captcha etc but at this stage all I want is to send the complete file. 我知道我需要对表单进行其他操作,例如验证,验证码等,但是在此阶段,我想要的只是发送完整的文件。

Below Is what I have so far, please excuse any formatting issues: 下面是我到目前为止所拥有的,请原谅任何格式问题:

HTML5 HTML5

<form role="form" action="addDocument.php" method="post" onsubmit="return validateForm()">
    <!-- file browse and upload-->
    <div class="form-group">
        <input type="file" name="file" class="file">
        <div class="input-group col-xs-12">

            <span class="input-group-addon"><i class="fa fa-file-image-o"></i></span>                                       
            <input type="text" class="form-control input-lg" disabled placeholder="Upload Image"> 

            <span class="input-group-btn">
                <button class="browse btn btn-primary input-lg" type="button"><i class="fa fa-search"></i> Browse</button>
            </span>
        </div>
   </div>
   <div class="form-group">
        <label class="sr-only" for="c-form-1-name">Name</label>
        <input id="c-form-1-name" class="c-form-1-name form-control" name="name" placeholder="Name..." type="text">
   </div>
   <div class="form-group">
        <label class="sr-only" for="c-form-1-email">Recipient Email</label>
        <input id="c-form-1-email" class="c-form-1-email form-control" name="remail" placeholder="Recipient Email..." type="text">
   </div>       
   <button class="btn btn-primary btn-lg btn-block" type="submit">Send</button> </form>

PHP PHP

<?php
ini_set('display_errors', 1);
$senderEmail = '<demo@mydomain.co.uk>'; 
$recipientEmail = filterInput($_POST['remail']); 
$message = 'You have been sent an electronic document via the Demo';
$subject = 'Demo';

echo $senderEmail.$recipientEmail.$message.$subject;
if(sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message))
{
    echo "Email sent successfully!";
}
else
{
    echo "Failed to send the email...";
}

function filterInput($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
function sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message){


    if(isset($_FILES)) {

echo "iamhere";
        $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","JPG","png","PNG","rtf","txt","xml");

        $files = array();
        foreach($_FILES as $name=>$file) {
            //die("file size: ".$file['size']);
            if($file['size']>=5242880)//5mb
            {
                $fileSize=$file['size'];
                return false;
            }
            $file_name = $file['name']; 
            $temp_name = $file['tmp_name'];
            $file_type = $file['type'];
            $path_parts = pathinfo($file_name);
            $ext = $path_parts['extension'];
            if(!in_array($ext,$allowedExtensions)) {
                return false;
                die("File $file_name has the extensions $ext which is not allowed");
            }

            //move the file to the server, cannot be skipped
            $server_file="/tmp/$path_parts[basename]";
            move_uploaded_file($temp_name,$server_file);
            array_push($files,$server_file);
            //array_push($files,$temp_name);
        }

        // email fields
        $headers = "From: $senderEmail";


        // boundary 
        $semi_rand = md5(time()); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

        // headers for attachment 
        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

        // multipart boundary 
        $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
        $message .= "--{$mime_boundary}\n";

        // preparing attachments
        for($x=0;$x<count($files);$x++){
            $file = fopen($files[$x],"rb");
            $data = fread($file,filesize($files[$x]));
            fclose($file);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
            "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            $message .= "--{$mime_boundary}\n";
        }

        // send
        return @mail($recipientEmail, $subject, $message, $headers);

    }
}   

Here you need to add enctype="multipart/form-data" to form if you want to post any file/image 如果要发布任何文件/图像,则需要在此处添加enctype="multipart/form-data"进行表单

from

<form role="form" action="addDocument.php" method="post" onsubmit="return validateForm()">

to

<form role="form" enctype="multipart/form-data" action="addDocument.php" method="post" onsubmit="return validateForm()">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM