Hey everyone,
Got this little snippet here:
<?php
// did files get sent
if(isset($_FILES) && (bool) $_FILES) {
// define allowed extensions
$allowedExtensions = array("pdf","doc","docx");
$files = array();
// loop through all the files
foreach($_FILES as $name=>$file) {
// define some variables
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
// check if this file type is allowed
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(!in_array($ext,$allowedExtensions)) {
die("extension not allowed");
}
// move this file to the server YOU HAVE TO DO THIS
$server_file = "/uploads/$path_parts[basename]";
move_uploaded_file($temp_name,$server_file);
// add this file to the array of files
array_push($files,$server_file);
}
// define some mail variables
$to = $_POST['email'];
$from = $_POST['name'];
$subject ="test attachment";
$msg = "here ya go";
$headers = "From: $from";
// define our boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// tell the header about the boundary
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
// part 1: define the plain text email
$message ="\n\n--{$mime_boundary}\n";
$message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .="Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
$message .= "--{$mime_boundary}\n";
// part 2: loop and define mail attachments
foreach($files as $file) {
$aFile = fopen($file,"rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n";
$message .= " name=\"$file\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$file\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send the email
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
echo "<p>location: $server_file</p>";
print_r ( $_FILES ); die();
} else {
echo "<p>mail could not be sent!</p>";
print_r ( $_FILES ); die();
}
die();
}
?>
<form method="POST" name="email_form_with_php" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p><label for="name">Your Name: </label> <input type="text" name="name" /></p>
<p><label for="email">Assessors Email: </label> <input type="text" name="email" /></p>
<p><label for="uploaded_file">Select A File To Upload:</label> <input type="file" name="uploaded_file" /></p>
<p><input type="submit" value="Submit" name="submit" /></p>
<p></p>
</form>
The problem is everytime I upload I get something along these lines:
mail sent to emailhere@email.com!
location: /uploads/PLTS BA Guidance notes.pdf
Array ( [uploaded_file] => Array ( [name] => PLTS BA Guidance notes.pdf [type] => application/pdf [tmp_name] => /tmp/phpMjqHg0 [error] => 0 [size] => 1006142 ) )
When I go to my emails I have a file which is 0kb with the name "/uploads/PLTS BA Guidance notes.pdf"
Thanks