/    Sign up×
Community /Pin to ProfileBookmark

php contact form with multiple file uploader (help!)

Hello all. I have been working very diligently trying to figure this out for myself but I have reached a point of stand-still. Basically, I have this (semi-weird) contact form that I’ve used for years adapted from an old learn-to-code book. Now I have this uploader (php/js) that I’m adding to it. Separately they work fine (at least to me) but I’m having issue trying to combine these 2 elements…

Specifically, I would really like the uploader to communicate back to the contact form the names of the uploaded files (quantity of upload can vary considerably) so that those names can be included in the generated email. Can someone please help in this endeavor!!!?

CONTACT FORM W/ UPLOADER

[CODE]<?php
if (array_key_exists(‘send’, $_POST)) {
// mail processing script
$to = ’[email protected]’;
$me = ’[email protected]’;
$subject = ‘Score Call’;

// list expected fields
$expected = array(‘name’, ‘age’, ‘gender’, ‘mailing’, ‘city’, ‘state’, ‘zipcode’, ‘phone’, ’email’, ‘title’, ‘duration’, ‘instrumentation’, ‘agreement’, ‘upl’);
// set required fields
$required = array(‘name’, ‘age’, ‘gender’, ‘mailing’, ‘city’, ‘state’, ‘zipcode’, ‘phone’, ’email’, ‘title’, ‘duration’, ‘instrumentation’, ‘agreement’);

// set additional headers
$headers = ‘From: <[email protected]>’;

// set the include
$process = ‘includes/application.inc.php’;
if (file_exists($process) && is_readable($process)) {
include($process);
}
else {
$mailSent = false;
mail($me, ‘Server Problem’, “$process cannot be read”, $headers);
}
}
?>
<!DOCTYPE html>
<html lang=”en-US”>
<head>
<meta charset=”UTF-8″>
<link href=”css/style.css” rel=”stylesheet”>
</head>

<body>
<div id=”appContainer”>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class=”warning”>Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class=”warning”>Sorry, there was a problem sending your message. Please try again later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class=”success”>Your application has been submitted. A member of Calliope&rsquo;s Call will be in touch! Thank you!</p>
<?php } ?>

<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post” name=”application” id=”application” onSubmit=”MM_validateForm(‘name’,”,’R’,’email’,”,’RisEmail’);return document.MM_returnValue”>

<table class=”applicationForm formText”>
<tr>
<td class=”formTextColOne”><label for=”name”>Full Name: <?php
if (isset($missing) && in_array(‘name’, $missing)) { ?>
<span class=”warning”>Please enter your Full Name</span><?php } ?>
</label>
<input name=”name” type=”text” class=”textInput” id=”name”
<?php if (isset($missing)) {
echo ‘value=”‘.htmlentities($_POST[‘name’], ENT_QUOTES).'”‘;
} ?>
></td>
<td class=”formTextColTwo”><label for=”age”>Age: <?php
if (isset($missing) && in_array(‘age’, $missing)) { ?>
<span class=”warning”>Please enter your Age</span><?php } ?>
</label>
<input name=”age” type=”text” class=”textInput” id=”age”
<?php if (isset($missing)) {
echo ‘value=”‘.htmlentities($_POST[‘age’], ENT_QUOTES).'”‘;
} ?>
></td>
<td class=”formTextColThree”><span class=”formText”>Gender:</span><br>
<select name=”gender” size=”1″>
<option value=”” selected>Please Select:</option>
<option value=”Male”>Male</option>
<option value=”Female”>Female</option>
</select><?php
if (isset($missing) && in_array(‘gender’, $missing)) { ?>
<span class=”warning”>Please Select Your Gender</span><?php } ?>
</td>
</tr>
<tr>
<td class=”formTextColOne”>&nbsp;</td>
<td class=”formTextColTwo”><input type=”submit” name=”send” id=”send” value=”Submit Application”></td>
<td class=”formTextColThree”>&nbsp;</td>
</tr>
</table>
</form>
<form id=”upload” method=”post” action=”upload.php” enctype=”multipart/form-data”>
<div id=”drop”>
<span style=”font-size:2em;”>Upload</span> your biography (please limit to 1 paragraph or 300 words), recording, and any applicable supporting documentation.

<a>Browse</a>
<input type=”file” name=”upl” multiple />
</div>

<ul>
<!– The file uploads will be shown here –>
</ul>
</form>
</div>
<!– JavaScript Includes –>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script>
<script src=”js/jquery.knob.js”></script>

<!– jQuery File Upload Dependencies –>
<script src=”js/jquery.ui.widget.js”></script>
<script src=”js/jquery.iframe-transport.js”></script>
<script src=”js/jquery.fileupload.js”></script>

<!– Our main JS file –>
<script src=”js/script.js”></script>

</body>
</html>[/CODE]

And then the application processor

[code=php]<?php

// create empty array for any missing fields
$missing = array();

// assume that there is nothing suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = ‘/Content-Type:|Bcc:|CC:/i’;
// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}

// check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);

if ($suspect ) {
$mailSent = false;
unset($missing);
}
else {
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}

// validate the email address
if (!empty($email)) {
// regex to identify illegal characters in email address
$checkEmail = ‘/^[^@]+@[^srn'”;,@%]+$/’;
// reject the email address if it doesn’t match
if (!preg_match($checkEmail, $email)) {
$suspect = true;
$mailSent = false;
unset($missing);
}
}

// validate the comments
// regex to identify illegal characters in email address
//$link = false;
//$checkComments = ‘/(http://|www)/’;
//if(preg_match($checkComments, stripcslashes($comments))){
// $link = true;
// $mailSent = false;
// unset($missing);
//}
//else{

// go ahead only if not suspect and all required fields OK
if (!$suspect && empty($missing)) {
// initialize the $message variable
$message = ”;
// loop through the $expected array
foreach($expected as $item) {
// assign the value of the current item to $val
if (isset(${$item})) {
$val = ${$item};
}
// if it has no value, assign ‘Not Selected’
else {
$val = ‘Not selected’;
}
// if an array, expand as comma-separated string
if (is_array($val)) {
$val = implode(‘, ‘, $val);
}
// add label and value to the message body
$message .= ucfirst($item).”: $valnn”;
}

// add FILES to message
//$newVal = implode (‘, ‘, $_FILES[‘upl’][‘name’]);
//$message .= ucfirst($_FILES[‘upl’][‘name’]).”: $newValnn”;

// limit line length
$message = wordwrap($message, 70);

// create Reply-To header
if (!empty($email)) {
$headers .= “rnReply-To: $email”;
}

// send it
$mailSent = mail($to, $subject, $message, $headers);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
// }

?>[/code]

And the, for now, separate uploading processor:

[code=php]<?php
(move_uploaded_file($_FILES[‘upl’][‘tmp_name’], ‘../public_ftp/incoming/’.$_FILES[‘upl’][‘name’].time()))
?>[/code]

I know I’m missing something stupid here but I’m stuck… Any help would be greatly appreciated!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@rothndauthorJun 04.2016 — Modifications I've made but still not fully functional:

I modified the PHP at the top of the original HTML page as follows:

move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'].time());

if (array_key_exists('send', $_POST)) {

$files = $_
FILES['upl'];

....[/QUOTE]


This is an effort to 1. combine the forms so to speak by having both 'action' back to the same page and 2. to try to make the names of the files available to the actual email generating script (application.inc.php). In this effort I also modified the uploading form to:

<form id="upload" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">

<div id="drop">

<span style="font-size:2em;">Upload</span> your biography (please limit to 1 paragraph or 300 words), recording, and any applicable supporting documentation.

<a>Browse</a>
<input type="file" name="upl" multiple />
</div>

<ul>
<!-- The file uploads will be shown here -->
</ul>
</form>[/QUOTE]


And finally, I updated the form/email processing script (application.inc.php) with the following:

$info = pathinfo($files);

$file_name = basename($files,'.'.$info['extension']);

$message .= ucfirst('Files').": $file_namenn";[/QUOTE]


Through all my research I feel like I'm on a much better/closer track to actually getting what I want but it's still not working. Output is simply "Files:" in the generated email which is telling me that it either doesn't have access to the original $_FILES['upl'] or that it isn't set up to iterate through the $_FILES['upl'] array properly.

Any help/thoughts would be greatly appreciated!
Copy linkTweet thisAlerts:
@rothndauthorJun 04.2016 — I found the solution.
Copy linkTweet thisAlerts:
@kvcodesApr 18.2017 — Playing with multiple is easy to work and upload with code easy way.

Multiple File Upload in PHP With Database

Here you can get more details about hadling multiple file uploads.
×

Success!

Help @rothnd 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.21,
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,
)...