我是新手,在搜索时,我可以找到我面临的问题。 我的问题是:我有这个表格,它应该上传文件,并作为邮件发送。如何显示正在上载的文件的状态。因为,人们只是在不让文件上传的情况下提交表单。 这是我的PHP代码:
<?php
if(isset($_POST['Email']))
{
$to = "<<email id>>";
$fromEmail = $_POST['Email'];
$fromName = $_POST['Name'];
$subject = $_POST['Subject'];
$mess= $_POST['Description'];
$message ="\n From: $fromName
\n Email: $fromEmail
\n Topic Name: $subject
\n Article Details: $mess
\n------------------------------------------------------------
";
/* Start of headers */
$headers = "From: $fromName";
if(isset($_FILES['attachment']['tmp_name']))
{
/* GET File Variables */
if($_FILES['attachment']['size']<25000000)
{
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if($_FILES['attachment']['size']>0)
{
$message .="\n There is an attachment along with this mail.\n";
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
}
else
{
}
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "<script type='text/javascript'>alert('Success');</script>";
}
else{
echo "Error in Email sending";
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Contact.css">
</head>
<body>
<div id="form-main">
<div id="form-div">
<form action="emailFileArticle.php" method="post" name="mainform" enctype="multipart/form-data" class="form">
<p class="title">
<input name="Subject" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Topic Name" id="Subject" required/>
</p>
<p class="text">
<textarea name="Description" class="validate[required,length[6,300]] feedback-input" id="Description" placeholder="Please mention somthing about the Video/image being uploaded" required></textarea>
</p>
<p class="name">
<input name="Name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="Name" required/>
</p>
<p class="email">
<input name="Email" type="email" class="validate[required,custom[email]] feedback-input" id="Email" placeholder="Email" required/>
</p>
<p class="upload">
<input name="attachment" type="file" id="attachment" multiple>
</p>
<div class="submit">
<input type="submit" value="send" name="Submit" id="button-blue"/>
<div class="ease"></div>
</form>
</div>
</div>
</body>
编辑:我正在寻找一种进度条解决方案,我得到了here,但最终得到了以下代码:
<?php
if(isset($_POST['Email']))
{
$to = "masterpratiksaboo@gmail.com,";
$fromEmail = $_POST['Email'];
$fromName = $_POST['Name'];
$subject = $_POST['Subject'];
$mess= $_POST['Description'];
$message ="\n From: $fromName
\n Email: $fromEmail
\n Topic Name: $subject
\n Article Details: $mess
\n------------------------------------------------------------
";
/* Start of headers */
$headers = "From: $fromName";
if(isset($_FILES['file1']['tmp_name']))
{
?>
<script>
function _(el){
return document.getElementById(el);
}
function uploadFile(){
var file = _("file1").files[0];
if(file.name=="" || file.name==null)
{alert("step break II");}
alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "fin.php");
ajax.send(formdata);
}
function progressHandler(event){
_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
}
function errorHandler(event){
_("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
_("status").innerHTML = "Upload Aborted";
}
</script>
<?php
echo "step break";
/* GET File Variables */
if($_FILES['file1']['size']<25000000)
{
$tmpName = $_FILES['file1']['tmp_name'];
$fileType = $_FILES['file1']['type'];
$fileName = $_FILES['file1']['name'];
if($_FILES['file1']['size']>0)
{
$message .="\n There is an attachment along with this mail.\n";
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
}
else
{
}
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "<script type='text/javascript'>alert('Thank you for the Article. Our Content Quality team shall review the post ASAP');</script>";
}
else{
echo "Error in Email sending";
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../Contact.css">
</head>
<body>
<div id="form-main">
<div id="form-div">
<form action="fin.php" method="post" name="mainform" enctype="multipart/form-data" class="form">
<p class="title">
<input name="Subject" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Topic Name" id="Subject" required/>
</p>
<p class="text">
<textarea name="Description" class="validate[required,length[6,300]] feedback-input" id="Description" placeholder="Please mention somthing about the Video/image being uploaded" required></textarea>
</p>
<p class="name">
<input name="Name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="Name" required/>
</p>
<p class="email">
<input name="Email" type="email" class="validate[required,custom[email]] feedback-input" id="Email" placeholder="Email" required/>
</p>
<p class="upload">
<input type="file" name="file1" id="file1"><br>
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</p>
<div class="submit">
<input type="submit" value="send" name="Submit" id="button-blue"/>
<div class="ease"></div>
</form>
</div>
</div>
</body>
</html>
我尝试了很多东西,但仍然无能为力。
答案 0 :(得分:0)
如果您想阻止发送没有附件的电子邮件,您应该更改“附件检查”
更改此行:
if(isset($_FILES['attachment']['tmp_name']))
要:
if(!empty($_FILES['attachment']['tmp_name']))