我刚刚设置了一个有效的上传表单提交。它经过测试,文件和内容完美无缺,直接发送到我的电子邮箱。
然而,我前几天再次测试它似乎已经停止工作,电子邮件正在通过,但没有数据传来,电子邮件只是空白。我不明白为什么会突然发生这种情况,或者我是否在意外中删除了代码中的内容?
代码( PHP ):
<?php
if(isset($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'My google recaptcha code';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$videolink = !empty($_POST['videolink'])?$_POST['videolink']:'';
$file = !empty($_POST['sfile'])?$_POST['sfile']:'';
$fname = $_FILES['sfile']['name'];
$message = !empty($_POST['message'])?$_POST['message']:'';
$mailto = "Email goes here";
$subject = 'Upload form submission';
$htmlContent = "<html>
<h1>Upload Submission Details:</h1>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Video link: </b>".$videolink."</p>
<p><b>File: </b>".$fname."</p>
<p><b>Message: </b>".$message."</p></html>
";
$uploaddir = 'tmp';
@mkdir($uploaddir,0777);
$name = $_FILES['sfile']['name'];
$name=$uploaddir."/".$name;
//$uploadfile = $uploaddir . basename($_FILES['file']['name']);
//$tmp_path = $_FILES["file"]["tmp_name"];
if(move_uploaded_file($_FILES['sfile']['tmp_name'],$name)) {
$file_size = filesize($name);
$handle = fopen($name, "r");
$mycontent = fread($handle, $file_size);
fclose($handle);
$mycontent = chunk_split(base64_encode($mycontent));
unlink($name);
//chunk_split(base64_encode(file_get_contents($_FILES["sfile"]["tmp_name"])));
//chunk_split(base64_encode($mycontent));
// a random hash will be necessary to send mixed content
$uid = md5(uniqid(time()));
$filename = basename($_FILES['sfile']['name']);
// header
$header = "From: \"FS Upload submission\" <\"Upload@submissions.com\">\r\n";
$header .= "Reply-To: ".$email."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/html; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $htmlContent."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $mycontent."\r\n\r\n";
$nmessage .= "--".$uid."--";
}
if($responseData->success):
if (mail($mailto, $subject, $nmessage, $header)) {
$succMsg = 'Mail request has submitted successfully.'; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
$name = '';
$email = '';
$videolink = '';
$file = '';
$message = '';
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please complete the reCAPTCHA form.';
endif;
else:
$errMsg = '';
$succMsg = '';
$name = '';
$email = '';
$videolink = '';
$file = '';
$message = '';
endif;
?>
代码( HTML&amp; PHP ):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/php; charset=UTF-8" />
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form enctype="multipart/form-data" action="" method=POST id=uploadform autocomplete=off>
<div class=top-row>
<div class=field-wrap>
<!-- Users name -->
<input id="sender" type="text" value="<?php echo !empty($name)?$name:''; ?>" placeholder="Your name" name="name" required>
</div>
<div class=field-wrap>
<!-- Users email -->
<input id=senderEmail type="email" value="<?php echo !empty($email)?$email:''; ?>" placeholder="Email@domain.com" name="email" required>
</div>
</div>
<div class=top-row>
<div class=field-wrap>
<!-- Users link -->
<input id="sender" type="text" value="<?php echo !empty($videolink)?$videolink:''; ?>" placeholder="Video Link" name="videolink">
</div>
<div class=field-wrap>
<!-- Users file -->
<input id="file" type="file" value="<?php echo !empty($file)?$file:''; ?>" name="sfile">
</div>
</div>
<textarea id=message type="text" placeholder=Description name=message spellcheck="true" required><?php echo !empty($message)?$message:''; ?></textarea>
<!-- Recaptcha -->
<center>
<div class="g-recaptcha" data-sitekey="My Google recaptcha code"></div>
</center>
<!-- Error messages -->
<?php if(!empty($errMsg)): ?>
<div class="errMsg"><?php echo $errMsg; ?></div>
<?php endif; ?>
<?php if(!empty($succMsg)): ?>
<div class="succMsg"><?php echo $succMsg; ?></div>
<?php endif; ?>
<center><input type=submit name=submit class="button button-block" value="SUBMIT"/></center>
</form>
</body>
</html>