php - multiple attachments are sent with phpmailer, I need to save to server only PDF's -
i have issue 1 page of rather large application. page has form attaches files user , emails them phpmailer. need add ability save attachments pdf's. have included section needs tweeking. new section in between lines of asterisks.
//handle files $total = count($_files['uploadfile']['tmp_name']); ($i = 0; $i < $total; $i++) { $name = $_files['uploadfile']['name'][$i]; $path = $_files['uploadfile']['tmp_name'][$i]; $type = $_files['uploadfile']['type'][$i]; $mail->addattachment($path,$name); //************************************************************************************* // new pdf check here $ext = pathinfo($path, pathinfo_extension); if ($ext == '.pdf'){ if (move_uploaded_file($_files['uploadfile']['tmp_name'][$i], "path/to/file/{$_files['uploadfile']['name']}")){ $pdfquery = "insert reports (case_id, user_id, filename, upload_date, dept_id) values ('$case_id', '$user', '{$_files['uploadfile']['name']}', curdate(), '$dept_id')"; $pdfresult = mysqli_query($dbc, $pdfquery) or trigger_error("query: $pdfquery\n<br>mysql error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1){ echo 'pdf case report saved.<br><br>'; } else { echo 'report failed save database.'; }//end affected rows } // end move report. } // end $ext check. //************************************************************************************* } // end loop.
you need check mime type, not extention (.pdf), since 1 can upload php file named evil.pdf server or might upload pdf having different extention. show part relevant question.
$total = count($_files['uploadfile']['tmp_name']); ($i = 0; $i < $total; $i++) { //attach file anyway $name = $_files['uploadfile']['name'][$i]; $path = $_files['uploadfile']['tmp_name'][$i]; $type = $_files['uploadfile']['type'][$i]; $mail->addattachment($path,$name); if (finfo_file(finfo_open(fileinfo_mime_type), $_files['file']['tmp_name']) !== 'application/pdf') { //file not have expected mime type, possibly hack //attempt, therefore ignore file continue; } //if program reaches point, file pdf }
Comments
Post a Comment