/    Sign up×
Community /Pin to ProfileBookmark

making multiple file attachments optional

Hi, I am a novice in php. I have created a script with the help of other experts like you people to attach multiple files in html form. The script is working perfectly fine. I want to make this attachment as optional. That means if the user attaches the files, the form is submitted with the attachments and if the user does not attaches files, the form is submitted without the attachments. Please somebody help me. Here is the php file. I am using phpmailer.

[code=php]
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require ‘class.phpmailer.php’;

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$to = “[email protected]”;
$mail->AddAddress($to);
$mail->From = $_POST[’email’];
$mail->FromName = $_POST[‘name_f’];
$mail->Subject = “Admission Form”;

$body = “<table>
<tr>
<th colspan=’2′>Admission Form</th>
</tr>

<tr>
<td style=’font-weight:bold’>Course :</td>
<td>”.$_POST[‘course’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Payment Mode :</td>
<td>”.$_POST[‘p_mode’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Payment options :</td>
<td>”.$_POST[‘p_option’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>First Name :</td>
<td>”.$_POST[‘name_f’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Middle Name : </td>
<td>”.$_POST[‘name_m’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Last Name : </td>
<td>”.$_POST[‘name_l’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>E-mail : </td>
<td>”.$_POST[’email’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Date of Birth : </td>
<td>”.$_POST[‘date’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Gender : </td>
<td>”.$_POST[‘gender’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Nationality : </td>
<td>”.$_POST[‘nationality’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Address : </td>
<td>”.$_POST[‘address_line1′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>City : </td>
<td>”.$_POST[‘city’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Zip Code : </td>
<td>”.$_POST[‘zip’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>State : </td>
<td>”.$_POST[‘state’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Country : </td>
<td>”.$_POST[‘country’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Mobile Number : </td>
<td>”.$_POST[‘mobile_number’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Father/Mother First Name : </td>
<td>”.$_POST[‘fname_f’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Father/Mother Middle Name : </td>
<td>”.$_POST[‘fname_m’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Father/Mother Last Name : </td>
<td>”.$_POST[‘fname_l’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Father/Mother Mobile No : </td>
<td>”.$_POST[‘mobile_number1′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Qualification : </td>
<td>”.$_POST[‘qualification’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>University : </td>
<td>”.$_POST[‘university’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Year : </td>
<td>”.$_POST[‘year’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Qualification : </td>
<td>”.$_POST[‘qualification_2′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>University : </td>
<td>”.$_POST[‘university_2′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Year : </td>
<td>”.$_POST[‘year_2′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Qualification : </td>
<td>”.$_POST[‘qualification_3′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>University : </td>
<td>”.$_POST[‘university_3′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Year : </td>
<td>”.$_POST[‘year_3′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Discipline Information (School) : </td>
<td>”.$_POST[‘discipline’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Discipline Information (Violation) : </td>
<td>”.$_POST[‘violation’].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>12th Mark List : </td>
<td>”.$_POST[‘attachment1′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>12th Passing Certificate : </td>
<td>”.$_POST[‘attachment2′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>10th Mark List : </td>
<td>”.$_POST[‘attachment3′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>10th Passing Certificate : </td>
<td>”.$_POST[‘attachment4′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Address Proof : </td>
<td>”.$_POST[‘attachment8′].”</td>
</tr>

<tr>
<td style=’font-weight:bold’>Photographs : </td>
<td>”.$_POST[‘attachment9’].”</td>
</tr>

<table>”;
$body = preg_replace(‘/\\/’,”, $body); //Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = “[email protected]”; // SMTP server
//$mail->Username = “[email protected]”; // SMTP server username
//$mail->Password = “password”; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo(“[email protected]”);
$mail->AltBody = “To view the message, please use an HTML compatible email viewer!”; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap

if(!empty($_FILES[‘fileToUpload’][‘tmp_name’]) && !empty($_FILES[‘fileToUpload’][‘name’])) {
foreach($_FILES[‘fileToUpload’][‘tmp_name’] as $key => $value)
$mail->AddAttachment($_FILES[‘fileToUpload’][‘tmp_name’][$key], $_FILES[‘fileToUpload’][‘name’][$key]);
}

$mail->IsHTML(true); // send as HTML
$mail->Send();
echo ‘Thank You. Your form has been submitted’;
} catch (phpmailerException $e) {
echo $e->errorMessage();
}

?>[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 24.2017 — Help you what? You didn't tell us of any problem. Just a lot of html.
Copy linkTweet thisAlerts:
@NogDogAug 24.2017 — I do not see anything in the posted code that requires a file to be uploaded, so if you are having a problem in that case, we'll need more info, such as what error message you get or other symptoms.
Copy linkTweet thisAlerts:
@ssaxenaauthorAug 26.2017 — Help you what? You didn't tell us of any problem. Just a lot of html.[/QUOTE]

Actually my code is to allow user to attach multiple files with my form. The files are being attached but I want to make this as optional. That is even if the user does not attach any file, the form should be submitted. Presently if the user attaches files, the form is coming in email with the attachments, but if the user does not attach any file, the form is not submitted and the message is "Could not access file".
Copy linkTweet thisAlerts:
@ginerjmAug 26.2017 — Ok - let's revise your terminology.

Files are not 'attached' to forms. They are specified in a form using an input element with type = 'file'. The files are then uploaded and made available to your PHP script for handling.

As for them being 'optional', that depends entirely upon your script's processing. If the script logic checks for the existence of the uploaded file and doesn't find it, it can decide whether to proceed without the file or not.

As for your last statement - the form is not coming in via email - it is a POST method that is delivering the data to your script. Perhaps you are talking about the email that your script will be sending, which will not necessarily have a form in it.

If you have a problem with the script not performing as you expect, it is up to you to alter it using some logic.
Copy linkTweet thisAlerts:
@NogDogAug 26.2017 — Maybe try this:
[code=php]
if (!empty($_FILES['fileToUpload']['tmp_name']) && !empty($_FILES['fileToUpload']['name'])) {
foreach ($_FILES['fileToUpload']['tmp_name'] as $key => $value) {
// added this IF block:
if (file_exists($_FILES['fileToUpload']['tmp_name'][$key])) {
$mail->AddAttachment($_FILES['fileToUpload']['tmp_name'][$key], $_FILES['fileToUpload']['name'][$key]);
}
}
}
[/code]
Copy linkTweet thisAlerts:
@ssaxenaauthorAug 28.2017 — Maybe try this:
[code=php]
if (!empty($_FILES['fileToUpload']['tmp_name']) && !empty($_FILES['fileToUpload']['name'])) {
foreach ($_FILES['fileToUpload']['tmp_name'] as $key => $value) {
// added this IF block:
if (file_exists($_FILES['fileToUpload']['tmp_name'][$key])) {
$mail->AddAttachment($_FILES['fileToUpload']['tmp_name'][$key], $_FILES['fileToUpload']['name'][$key]);
}
}
}
[/code]
[/QUOTE]


Thanks NogDog.

You have solved my problem. You are really an expert. I am really thankful to you. Saxena
×

Success!

Help @ssaxena 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...