I have a form that will allow upload of multiple image files like below. Only 1 upload is required the rest is optional.
[code=html]
<form method=”post” enctype=”multipart/form-data” action=”upload.php”>
<input type=”file” name=”photo[]” class=”file” />
<input type=”file” name=”photo[]” class=”file” />
<input type=”file” name=”photo[]” class=”file” /></form>
Validate with getimagesize and send with phpmailer.
[code=php]<?php
ob_start();
require(“class.phpmailer.php”);
$errors = array();
if (‘POST’ === $_SERVER[‘REQUEST_METHOD’])
{
$firstname = sanitize($_POST[‘firstname’]);
$lastname = sanitize($_POST[‘lastname’]);
$email = sanitize($_POST[’email’]);
if (empty($firstname))
{
$errors[‘firstname’] = “Please provide first name.”;
}
if (empty($lastname))
{
$errors[‘lastname’] = “Please provide last name.”;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[’email’] = “Please provide a valid email address.”;
}
if (count($errors) === 0)
{
$imageinfo = array();
$my_files = $_FILES[‘photo’][‘tmp_name’];
foreach($my_files as $single_file) {
if(!empty($single_file)) {
$imageinfo[$single_file] = getimagesize($single_file);
if ($single_file[‘mime’] != ‘image/png’ && $single_file[‘mime’] != ‘image/jpeg’)
{ echo “Invalid Image File”;
exit();
} }
}
foreach($_FILES[‘photo’][‘tmp_name’] as $photo)
if(!empty($photo)) {
$mail->AddAttachment($photo);
$message = ‘some message’;
$mail = new PHPMailer();
$mail->SetFrom($email);
$mail->AddAddress($from);
$mail->Subject = “Submitted”;
$mail->Body = $message;
$mail->WordWrap = 50;
}
$mail->Send();
header(“Location: thankyou.php”);
exit();
}}
function sanitize($value)
{
return trim(strip_tags($value, $problem=”));
}
?>
My problem is I don’t see the invalid image file message when I attached a non image file but I received the email with no attachment either I attach image or non image. Please help. Thanks.