/    Sign up×
Community /Pin to ProfileBookmark

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>[/code]

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=”));
}
?>[/code]

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.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @phpnewbie08 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...