If you have some contact form(or just any form) where you need to have ‘upload file’ field(s) and you must do that in PHP – this article might help you.
Almost every tutorial i found while googling for a solution is a lot of spaghetti code. That’s why i decided to use swiftmailer, which makes code pretty self explanatory – so dig in.
Library used is swiftmailer which you need to download and upload to your server. Also it has with excellent docs.
require_once 'swiftmailer/swift_required.php';
if ( // Maybe add some validation here, like check for value of some hidden field or whatever ) {
// Catch your $_POST
$yourName = $_POST['yourName'];
// etc
// File validation
$maxFileSize = 10240; // file size in KB
$maxFileSizeMb = $maxFileSize / 1024; // file size in MB
// Specify what extension you are accepting for your file
$allowedExtensions = array("jpg", "jpeg", "tiff", "pdf", "doc", "docx");
$uploadedFileName = basename($_FILES['yourFile']['name']);
$uploadedFileType = substr($uploadedFileName, strrpos($uploadedFileName, '.') + 1);
$uploadedFileSize = $_FILES["yourFile"]["size"] / 1024;
if ( $uploadedFileSize > $maxFileSize ) {
$errors .= "Size of file should be less than $maxFileSizeMb MB \r\n";
} else {
$extensionAllowed = false;
for ( $i = 0; $i < sizeof($allowedExtensions); $i++ ) {
if ( strcasecmp($allowedExtensions[$i], $uploadedFileType) == 0 ) {
$extensionAllowed = true;
}
}
if ( ! $extensionAllowed ) {
$errors .= "The uploaded file is not supported file type. Only the following file types are supported: " . implode(',',$allowedExtensions) . " \r\n";
} else {
$pathOfUploadedFile = "some-writable-directory/$uploadedFileName";
$tmp_path = $_FILES["yourFile"]["tmp_name"];
if ( is_uploaded_file($tmp_path) ) {
if( ! copy($tmp_path, $pathOfUploadedFile) ) {
$errors .= "error while copying the uploaded file \r\n";
} else {
// Set up message text
$messageText = "Name: $yourName \r\n";
// etc
// Start up swiftmailer
$transport = Swift_MailTransport::newInstance();
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance("Your name: $yourFullName")
->setFrom(array($yourEmail => $yourFullName ) )
->setTo(array("mail@mail.com" => "John Doe"))
->setBody($messageText)
->attach(Swift_Attachment::fromPath( $pathOfUploadedFile ));
//Send the message
if( $mailer->send($message) ) {
// if mail has been sent -> delete uploaded file from your server
unlink($pathOfUploadedFile);
}
}
} else {
$errors .= "tmp_path fail \r\n";
}
}
}
}