SRL Archive - Web Development

php, email, attachments how do I add this?


grats

2012-06-05 02:31:33

[PHP] <?php
$allowedExtensions = array("pdf", "docx", "txt"); //file extensions allowed
foreach ($_FILES['attach'] as $file) {
if ($file['tmp_name'] > '') {
$fileExtension=pathinfo($file, PATHINFO_EXTENSION);
if (!in_array($fileExtension, $allowedExtensions)) {
die($file['name'].' is an invalid file type!');
}
}
$size = $file['size'];
$MAX_FILE_SIZE = 8388608; //8 megabytes
if ($size >= $MAX_FILE_SIZE) {
die('Your file(s) is/are too big!');
}
}





//tmp files/attachments names

$attach0 = $_FILES["attach"]["tmp_name"][0];

$attach1 = $_FILES["attach"]["tmp_name"][1];

$attach2 = $_FILES["attach"]["tmp_name"][2];

$attach3 = $_FILES["attach"]["tmp_name"][3];

$attach4 = $_FILES["attach"]["tmp_name"][4];

//attachment's name with extension
$attach0name = basename($_FILES['attach']['name'][0]);
$attach1name = basename($_FILES['attach']['name'][1]);
$attach2name = basename($_FILES['attach']['name'][2]);
$attach3name = basename($_FILES['attach']['name'][3]);
$attach4name = basename($_FILES['attach']['name'][4]);

//get files extension
$attach0ext = end(explode('.', $attach0name));
$attach1ext = end(explode('.', $attach1name));
$attach2ext = end(explode('.', $attach2name));
$attach3ext = end(explode('.', $attach3name));
$attach4ext = end(explode('.', $attach4name));


$fileext = array("".$attach0ext."","".$attach1ext."","".$attach2ext."","".$attach3ext."","".$attach4ext."");

$files = array("".$attach0."","".$attach1."","".$attach2."","".$attach3."","".$attach4."");[/PHP]

I need to add the fileext array to the attachments setup:
[PHP]// setup attachments

for($x=0;$x<count($files);$x++){

$file = fopen($files[$x],"rb");

$data = fread($file,filesize($files[$x]));

fclose($file);

$data = chunk_split(base64_encode($data));

$message .= "Content-Type: {\"application/octet-stream\"};".PHP_EOL."" . " name=\"$files[$x]\"".PHP_EOL."" .

"Content-Disposition: attachment;".PHP_EOL."" . " filename=\"$files[$x]\"".PHP_EOL."" .

"Content-Transfer-Encoding: base64".PHP_EOL.PHP_EOL."" . $data . "".PHP_EOL.PHP_EOL."";

$message .= "–{$mime_boundary}".PHP_EOL."";

}[/PHP]

I cannot add this array to the $files because if it has extensions then it doesn't upload the tmp file, it uploads some file that doesn't exist, therefore I need to add the extensions to the email after the correct files are located and attached, they then get an extension.. I assume that would work

so basically right now I get the phpTEMPFILE (random name) in email, I get 5 of them with no extension, manually adding the extension works but I would like it to be automated

or do I have to move the file and then attach & delete it afterward?