Php 与excel表单一起上载多个文件

Php 与excel表单一起上载多个文件,php,forms,Php,Forms,我们目前有一个表单,当提交用户响应时,将作为附件excel文件通过电子邮件发送给我们。我们还有另一个表单,允许用户上传多种类型的文件,然后作为附件通过电子邮件发送给我们。但是,我们无法将这两种形式合并为一种形式,以便我们可以接收包含这两种附件的电子邮件。我们是初学者 以下是: 1.附加单个excel内容的现有表单 2.现有的mailer.php 3.上载多个文件的现有表单 1。excel内容表单 <?php error_reporting(E_ALL ^ E_NOTICE); ob_

我们目前有一个表单,当提交用户响应时,将作为附件excel文件通过电子邮件发送给我们。我们还有另一个表单,允许用户上传多种类型的文件,然后作为附件通过电子邮件发送给我们。但是,我们无法将这两种形式合并为一种形式,以便我们可以接收包含这两种附件的电子邮件。我们是初学者

以下是: 1.附加单个excel内容的现有表单 2.现有的mailer.php 3.上载多个文件的现有表单

1。excel内容表单

<?php

error_reporting(E_ALL ^ E_NOTICE); 

ob_start();


$taxyear = $_POST['taxyear'];
$company = $_POST['company'];
$tfn = $_POST['tfn'];
$director1 = $_POST['director1'];
$director2 = $_POST['director2'];
$email = $_POST['email'];


?>

<html>
<body> 
<form>
<table width="720" border="0" align="center" bgcolor="#FFFFFF">

  <tr align="center" valign="top">
    <td height="1730">COMPANY RETURN
      <table width="702" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td colspan="6" align="left" class="whiteonred" >Company <strong><? echo $taxyear; ?> </strong></td>
        </tr>
          <tr class="excel7" style="height:18.0pt;">
          <td colspan="4" align="left" class="centgothicstd" ><strong>PRINCIPAL DETAILS </strong></td>
          <td colspan="2" align="right" class="centgothicsm">&nbsp;</td>

        </tr>
          <tr style="height:18.0pt;">
            <td align="left" class="centgothicsm" >COMPANY NAME &amp; TFN</td>
            <td colspan="3" align="left" class="borders"><strong><? echo $company; ?></strong></td>
            <td colspan="2" align="left" class="borders"><strong><? echo $tfn; ?></strong></td>

          </tr>
          <tr style="height:18.0pt;">
            <td width="213" align="left" class="centgothicsm" style="height:18.0pt;">DIRECTOR NAMES</td>
            <td colspan="3" align="left" class="borders"><? echo $director1; ?></td>
            <td colspan="2" align="left" class="borders"><? echo $director2; ?></td>

          </tr>
        <tr style="height:18.0pt;">
          <td width="213" align="left" class="centgothicsm" style="height:18.0pt;">EMAIL ADDRESS</td>
          <td colspan="5" align="left" class="borders"><? echo $email; ?></td>

        </tr>

      </table></td>
  </tr>
</table>
</form>
</body>
</html>

<?php

$FILE_CONTENTS = ob_get_contents(); 

ob_clean(); 

include("mailer.php"); 

$recipient = "darren@eto.net.au"; 

$subject = "Company submission"; 

$myEmail = new EPDEV_Emailer($recipient, $email, $subject); 

$myEmail->addFile("{$company}-{$taxyear}.xls", "application/vnd.ms-excel", $FILE_CONTENTS); 

$myEmail->send(); 

Header("Location: thankyouapplic.htm");
?>
<body>
<td height="353" colspan="9" align="left" valign="top">

  <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <tr>
      <td width="300" align="center">&nbsp;</td>

      <table width="500" height="407" border="0" align="center">
        <tr>
          <td width="120" align="left" class="indextextlight">Name:</td>
          <td colspan="2"><input name="fname" type="text" class="indextextlight" size="30" /></td>
          </tr>
        <tr>
          <td width="120" height="24" align="left" class="indextextlight">E-mail:</td>
          <td colspan="2"><label>
            <input name="email" type="text" class="indextextlight" size="30" />
          </label></td>
          </tr>
        <tr>
          <td width="120" align="left" class="indextextlight">Telephone:</td>
          <td colspan="2"><label>
            <input name="tel" type="text" class="indextextlight" id="tel" value="" size="15" />
          </label></td>
          </tr>
        <tr>
          <td width="120" align="left" valign="top" class="indextextlight">Message:

            </td>
          <td colspan="2" valign="top"><textarea name="comments" " id="comments" cols="45" rows="4"></textarea></td>
          </tr>
        <tr>
          <td width="120" valign="middle" class="indextextlight">Upload Files:</td>
          <td colspan="2"><p>
            <input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
            <br />
            <span class="centgothicmini">              note - hold the Ctrl key to select multiple files</span><br />
            <span class="centgothicmini">              note - max total size of all files can not  exceed 10mb</span></td>
          </tr>
        <tr>
          <td width="120" align="left" class="indextextlight">Send:</td>
          <td colspan="2" align="left"><input type="image" name="submit" value="Send Email" src="images/btnSubmit.gif" /></td>
          </tr>
        <tr>
          <td width="120" class="indextextlight">Result:</td>
          <td colspan="2" class="header"><?php
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showform(){
?></td>
          </tr>
        <tr>
          <td class="indextextlight">&nbsp;</td>
          <td colspan="2" class="header">&nbsp;</td>
        </tr>
        <tr><td colspan="3" align="right" valign="baseline" class="footer"><span class="footerheadings">&nbsp;</span> © 2009-13 BC Accountants Australia Pty Ltd</td>
        </tr>
        </table>

  </form>   
  <script type="text/javascript">
var frmvalidator  = new Validator("send");
frmvalidator.addValidation("fname","req","Please enter your Name");
frmvalidator.addValidation("email","maxlen=50");
frmvalidator.addValidation("email","req");
frmvalidator.addValidation("email","email");
frmvalidator.addValidation("tel","maxlen=15");
frmvalidator.addValidation("tel","numeric");

</script>      
  <?php
}

function sendMail() {
  if (!isset ($_POST['email'])) { //Oops, forgot your email addy!
    die ("<p>Oops!  You forgot to fill out the email address! Click on the back arrow to go back</p>");
  }
  else {

    $fname = stripslashes($_POST['fname']);
    $email = $_POST['email'];
    $phone = $_POST['tel'];
    $comments = stripslashes($_POST['comments']);
    $headers .= "\n";

    //Uniqid session
    $strSid = md5(uniqid(time()));

    //Let's start our headers
    $headers = "From: $fname<" . $_POST['email'] . ">\n";
    $headers .= "Reply-To: <" . $_POST['email'] . ">\n"; 
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
    $headers .= "This is a multi-part message in MIME format.\n";

    $headers .= "--".$strSid."\n";
    $headers .= "Content-type: text/html; charset=utf-8\n";
    $headers .= "Content-Transfer-Encoding: 7bit\n\n";  

    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $headers .= "<table>";

    $headers .= "<tr><td>CONTACT FORM</td><td>&nbsp;</td></tr>";
    $headers .= "<tr><td>Check for attachments</td><td>&nbsp;</td>      </tr>";

    $headers .= "<tr>
    <td>&nbsp;</td>
    <td > </td>
    </tr>";

    $headers .= "<tr>
    <td >Name:  " . strip_tags($_POST["fname"]) . "</td><td >&nbsp;</td>
    </tr>";

    $headers .= "<tr>
    <td>Email:  " . strip_tags($_POST["email"]) . "</td><td >&nbsp;</td>
    </tr>";

    $headers .= "<tr>
    <td>Phone:  " . strip_tags($_POST["phone"]) . "</td><td>&nbsp;</td>
    </tr>";

    $headers .= "<tr>
    <td>&nbsp;</td>
    <td > </td>
    </tr>";

    $headers .= "<tr>
    <td>MESSAGE</td><td>&nbsp;</td></tr>";

    $headers .= "<tr> <td COLSPAN = '2'>" . strip_tags($_POST["comments"]) . "</td>
    </tr>"; 

    $headers .= "<tr>
    <td>&nbsp;</td>
    <td > </td>
    </tr>";

    $headers .= "</table>";
    $headers .= "</body></html>";
    $headers .= "\n"; 



    //**multi attach**//
    for($i=0;$i<count($_FILES["attachment"]["name"]);$i++)
    {
    if($_FILES["attachment"]["name"][$i] != "")
    {
        $file_name = $_FILES["attachment"]["name"][$i];
        $data = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
        $headers .= "--".$strSid."\n";
        $headers .= "Content-Type: application/octet-stream;\n\tname=\"" . $file_name . "\"\n";
        $headers .= "Content-Transfer-Encoding: base64\n";
        $headers .= "Content-Disposition: attachment;\n\tfilename=\"" . $file_name . "\"\n\n";
        $headers .= $data."\n\n"; //The base64 encoded message
    }

    }
    $headers .= "\n"; 
    $headers .= "------=MIME_BOUNDRY_main_message--\n"; 
    $subject .= "Contact Form";
    // send the message
    mail("darrenmillbca@gmail.com", $subject, $message, $headers); 
    print "Mail sent. Thank you!";
$to = $email; 

$subject = "Contact Form"; 

$message = '
<html>
<body>

<p>Dear '; 
$message .= $fname;
$message .= '</p>
<p>Thankyou for your message. </p>
<p>Our staff will get back to you shortly. </p>

<p>Kind Regards <br />
  <br />
  </p>

</body>
</html>
';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: darrenmillbca@gmail.com' . "\r\n";
//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);



  }
}

switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}


?>

</html>

公司回报
公司
主要细节
公司名称及名称;TFN


导演姓名
电子邮件地址
*二,。php代码*


你们所要求的在这里是一个相当重要的改变,所以我会尽我最大的努力让它对你们来说很简单。您的excel内容表单使用电子邮件包装器(mailer.php),它将附加文件转换为电子邮件,只需$myEmail->addFile(“文件名”、“mimetype”、“文件内容”)。另一方面,上传表单没有使用这个包装器,而是单独生成一封完整的电子邮件。因此,您需要将重点放在将上载转移到使用包装器上;这将内容从上传表单移动到excel表单

因此,我们的重点是上传文件中的以下块:

for($i=0;$i<count($_FILES["attachment"]["name"]);$i++)
    {
    if($_FILES["attachment"]["name"][$i] != "")
    {
        $file_name = $_FILES["attachment"]["name"][$i];
        $data = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
        $headers .= "--".$strSid."\n";
        $headers .= "Content-Type: application/octet-stream;\n\tname=\"" . $file_name . "\"\n";
        $headers .= "Content-Transfer-Encoding: base64\n";
        $headers .= "Content-Disposition: attachment;\n\tfilename=\"" . $file_name . "\"\n\n";
        $headers .= $data."\n\n"; //The base64 encoded message
    }
到目前为止还不错-在这一点上,您的excel表单可以处理多个上载,但它无法创建它们。要解决这个问题,您需要开始传输html,允许用户将文件上传到excel表单。这就是这一部分:

<tr>
          <td width="120" valign="middle" class="indextextlight">Upload Files:</td>
          <td colspan="2"><p>
            <input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
            <br />
            <span class="centgothicmini">              note - hold the Ctrl key to select multiple files</span><br />
            <span class="centgothicmini">              note - max total size of all files can not  exceed 10mb</span></td>
          </tr>
        <tr>

上载文件:


注意-按住Ctrl键可选择多个文件
注意-所有文件的最大总大小不能超过10mb

您可以做的一件简单的事情是将其转储到excel表单上的任何位置,这样您的excel表单就可以接受多次上载。从这里开始,只需将您想要的任何其他字段从上传转移到excel:)

谢谢。不幸的是,我仍然有问题,我想这是因为内容和上传都是从用户那里收集到初始html文件中的。现在的问题是如何将上传的文件传递到excel php文件?这是初始文件;您可以通过将上传文件中至少上述html部分复制到excel文件,将两个html文件合并在一起,即将两个表单合并为一个表单;如果这不可能,那么您必须将excel文件放入某个临时文件(文件内容可能对您有用)中,并从上载表单中的文件(文件内容)中提取它。Yes尝试合并,但失败了,无论如何,我们希望隐藏php代码,因为我们最近被复制了。我将尝试第二个建议。如果你可以付费,请发邮件给我。很遗憾,堆栈溢出不能直接给你发邮件。你们有网站吗?
for($i=0;$i<count($_FILES["attachment"]["name"]);$i++){
        if($_FILES["attachment"]["name"][$i] != "")
        {
            $file_name = $_FILES["attachment"]["name"][$i];
            $data = file_get_contents($_FILES["attachment"]["tmp_name"][$i]);
            $myEmail->addFile($file_name,"application/octet-stream",$data);
        }
}
$myEmail->addFile("{$company}-{$taxyear}.xls", "application/vnd.ms-excel", $FILE_CONTENTS); 
 // Drop it right here.
$myEmail->send();
<tr>
          <td width="120" valign="middle" class="indextextlight">Upload Files:</td>
          <td colspan="2"><p>
            <input name="attachment[]" type="file" multiple="" class="indextextlight" size="42">
            <br />
            <span class="centgothicmini">              note - hold the Ctrl key to select multiple files</span><br />
            <span class="centgothicmini">              note - max total size of all files can not  exceed 10mb</span></td>
          </tr>
        <tr>