PHPMailer带有一个输入按钮的多个附件

时间:2015-09-03 12:52:53

标签: php html forms phpmailer attachment

我正在尝试制作一个能够支持多个图像附件的联系表单。

我收到了here的联系表格,一切正常。我真的想添加在单个输入元素中附加多个图像的功能。

联系表格的顶部是这个php:

dbaccess stores lq.sql
real    0m0.701s
user    0m0.036s
sys     0m0.032s

这是表格的html(我把它剪下来所以它只是表格的上传部分):

<?PHP

require_once("./include/fgcontactform.php");

$formproc = new FGContactForm();    
$formproc->AddRecipient('****@****.com');
$formproc->SetFormRandomKey('************');
$formproc->AddFileUploadField('photo','jpg,jpeg,gif,png,bmp',4000);

if(isset($_POST['submitted']))
{
   if($formproc->ProcessForm())
   {
        $formproc->RedirectToURL("thank-you.php");
   }
}
?>

我设法通过在初始PHP的顶部添加以下内容并在html中添加具有相关名称的新输入元素来实现多个图像上传工作:

<html>
<head>
    <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
    <script type='text/javascript' src='scripts/fg_captcha_validator.js'></script>
</head>
<body>

<form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>

   <h4><label for='photo' >Please upload your images:</label><br/>
    <input type="file" name='photo' id='photo' multiple="multiple"/><br/>

<span style="color:#999999;font-size:12px;">(To select more than 1 image hold the "CTRL" key as you click. If you're on Mac hold the "cmd" key.)</span><br/></h4>
    <span id='contactus_photo_errorloc' class='error'></span>

</form>

<script type='text/javascript'>
// <![CDATA[

    var frmvalidator  = new Validator("contactus");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();
    frmvalidator.addValidation("name","req","Please provide your name");

    frmvalidator.addValidation("email","req","Please provide your email address");

    frmvalidator.addValidation("email","email","Please provide a valid email address");

    frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");

    frmvalidator.addValidation("photo","file_extn=jpg;jpeg;gif;png;bmp","Upload images only. Supported file types are: jpg,gif,png,bmp");
// ]]>
</script>
</body>
</html>

虽然这样可行但我更喜欢只有一个输入按钮,并允许人们用它附加多个图像(最多约10个,但这是另一个时间的问题)。

我相信这是构成电子邮件的php,如果有帮助:

$formproc->AddFileUploadField('photo2','jpg,jpeg,gif,png,bmp',4000);
$formproc->AddFileUploadField('photo3','jpg,jpeg,gif,png,bmp',4000);
$formproc->AddFileUploadField('photo4','jpg,jpeg,gif,png,bmp',4000);
$formproc->AddFileUploadField('photo5','jpg,jpeg,gif,png,bmp',4000);

1 个答案:

答案 0 :(得分:0)

我为多个文件/图片上传所做的是使用IFRAME作为隐藏表单,文件输入会在您按下原始表单中包含的按钮后触发。表单在iframe中提交(因此不会重新加载您的页面)并上传文件,您可以获取文件的详细信息并使用javascript将其添加到您的表单中(例如,pic1.jpg已上传,pic2.png已上传... )我有一个代码示例,但我将不得不寻找它,所以请尝试按照此策略自行提出代码。