Stuck again. I have now implemented file upload, pdf generation and having a mail sent with the generated pdf and the file(s) uploaded.
The user fills out the form in send2.php and the submitted data is then processed in send3.php.
Now I'd like to implement a bit of security on the file uploaded (check for size, file type) before the data submitted/uploaded is processed by send3.php. I'd like to implement the check so that the user stays on send2.php, preferably with already entered form data intact, when he tries to upload a file that fails a check.
What I am having trouble wrapping my head around is how to implement the check on send2.php before the form data is sent to send3.php upon succesful validation.
Any help is much appreciated.
Here are the files.
Send2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["skema"]["name"].value;
if (x==null || x=="")
{
alert("Du skal skrive dit navn.");
return false;
}
}
</script>
</head>
<body>
<form action="send3.php" method="post" name="skema" onsubmit="return validateForm()" enctype="multipart/form-data">
<fieldset>
<label for="name">Navn:</label>
<input type="text" name="name" value="" size="25"><br>
<label for="emailaddress">E-mail:</label>
<input type="text" name="emailaddress" value="" size="25"><br>
<label for="fileupload">Bilag 1:</label>
<input type="file" name="fileupload1" id="file1"><br>
<label for="fileupload">Bilag 2:</label>
<input type="file" name="fileupload2" id="file2"><br>
<input type="hidden" value="submitted">
<button type="submit">Submit</button>
</form>
</body>
</html>
Send3.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<html>
<?php
// download fpdf class (http://fpdf.org)
require("fpdf.php");
// fpdf object
$pdf = new FPDF();
// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, $_POST['name']);
// email stuff (change data below)
$to = $_POST['emailaddress'].",webmaster@admin12344321.com";
$from = "webmaster@admin12344321.com";
$subject = "Your form data";
$message = "<p>Here is your form data.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment PDF
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator.$eol;
// VEDHAEFTNING1
if($_FILES["fileupload1"]["name"] != "")
{
$strFilesName = $_FILES["fileupload1"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileupload1"]["tmp_name"])));
$headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $strContent.$eol.$eol;
$headers .= "--".$separator.$eol;
}
// VEDHAEFTNING2
if($_FILES["fileupload2"]["name"] != "")
{
$strFilesName = $_FILES["fileupload2"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileupload2"]["tmp_name"])));
$headers .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $strContent.$eol.$eol;
$headers .= "--".$separator.$eol;
}
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);
?>
</body>
</html>