我创建了一个表单,用户可以在其中输入数据并上传文件(form1.php)。单击提交按钮后,数据将插入数据库,上传的文件将保存到服务器(insert.php)中,并预览用户输入的字段。确认后,将发送一封带有附件的邮件。
**FORM1.PHP**
<form action="insert.php" action = "form-to-email.php" name="frmAdd" method="post" enctype="multipart/form-data">
<div class="form-inline clearfix">
<label class="col-md-5">Select A File To Upload:</label>
<div class="col-md-4">
<input type="file" name="myfile[]" id="Uploaded_file" multiple=""/>
</div>
</div><br/>
<div class="form-inline clearfix">
<label class="col-md-5">Name of the Administrator </label>
<div class="col-md-5">
<input type="text" name="Admin_name" class="form-control" Required><br/>
</div>
</div><br/>
**INSERT.PHP**
<?php
//If directory doesnot exists create it.
$output_dir = "uploads/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
$ImageType = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
//$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$ImageName = $ImageName;
//$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $ImageName);
//echo "<br> Error: ".$_FILES["myfile"]["error"];
$ret[$fileName]= $output_dir.$NewImageName;
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
$ImageType = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
//$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$ImageName = $ImageName;
// $ImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
$result[$ImageName]=$ImageName;
//$ret[$NewImageName]= $output_dir.$ImageName;
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$ImageName );
}
}
}
echo $uploadresult=json_encode($result);
}
?>
/*Hidden fields to carry the file name to the next phppage to send as an attachment */
<form action="mail1.php" method="Post" >
<input type="text" name="Upload1" value="<?php echo ($uploadresult); ?>">
</form>
**MAIL1.PHP**
<?php
$Upload1=$_POST['Upload1'];
$filepath="C:/app/apache24/htdocs/Uploads/";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email_body = "body";
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "***********";
$mail->SetFrom('******');
$mail->Subject = "System Configuration ". $formno ."";
$mail->MsgHTML($email_body);
//$address="*********";
$mail->AddAddress($address);
$mail->addAttachment("uploads/$Upload1");
if($mail->Send())
{
echo "Success";
echo $Upload1;
}
else echo "ERROR IN SENDING MAILS";
}
exit;
?>
我的问题是,如何将这些文件名发送到 mail1.php 以便它们可以附加并在邮件中发送?
感谢任何建议:) 谢谢!:)