/    Sign up×
Community /Pin to ProfileBookmark

I’m trying to add a file upload to php form

I trying to add a file upload button to a php form to either email or upload to a directory on a our server. I cant figure this one out.

[B]careers.htm code:[/B]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd“>
<html>
<head>
<title>Advertising at Hope Today </title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<style type=”text/css”>
<!–
.style11 {font-size: 12px; color: #444; font-family: Arial;}
.style14 {color: #444444;
font-weight: bold;
}
.style15 {font-size: medium}
–>
</style>
<script language=”JavaScript” type=”text/JavaScript”>
<!–
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(“?”))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors=”,args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!=””) {
if (test.indexOf(‘isEmail’)!=-1) { p=val.indexOf(‘@’);
if (p<1 || p==(val.length-1)) errors+=’- ‘+nm+’ must contain an e-mail address.n’;
} else if (test!=’R’) { num = parseFloat(val);
if (isNaN(val)) errors+=’- ‘+nm+’ must contain a number.n’;
if (test.indexOf(‘inRange’) != -1) { p=test.indexOf(‘:’);
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+=’- ‘+nm+’ must contain a number between ‘+min+’ and ‘+max+’.n’;
} } } else if (test.charAt(0) == ‘R’) errors += ‘- ‘+nm+’ is required.n’; }
} if (errors) alert(‘The following error(s) occurred:n’+errors);
document.MM_returnValue = (errors == ”);
}
//–>
</script>
</head>

<body>

<form method=”POST” action=”mailertestnewAd.php”>
<p>Advertisiting </p>
<div align=”left”>
<table width=”291″ height=”212″ border=”0″>
<tr>
<td>Email:</td>
<td>&nbsp;</td>
<td><input name=”email” type=”text” onSelect=”MM_validateForm(’email’,”,’RisEmail’);return document.MM_returnValue” size=”25″></td>
</tr>
<tr>
<td width=”60″>Company Name </td>
<td width=”3″>&nbsp;</td>
<td width=”221″><input name=”cname” type=”text” id=”cname” onSelect=”MM_validateForm(’email’,”,’RisEmail’);return document.MM_returnValue” size=”25″></td>
</tr>
<tr>
<td>Name: </td>
<td>&nbsp;</td>
<td><input name=”name” type=”text” id=”name” size=”25″></td>
</tr>
<tr>
<td>Address: </td>
<td>&nbsp;</td>
<td><input type=”text” name=”address” size=”25″></td>
</tr>
<tr>
<td>City: </td>
<td>&nbsp;</td>
<td><input type=”text” name=”city” size=”25″></td>
</tr>
<tr>
<td>State: </td>
<td>&nbsp;</td>
<td><input type=”text” name=”state” size=”25″></td>
</tr>
<tr>
<td>Zip:</td>
<td>&nbsp;</td>
<td><input type=”text” name=”zip” size=”25″></td>
</tr>
<tr>
<td>Phone: </td>
<td>&nbsp;</td>
<td><input type=”text” name=”phone” size=”25″></td>
</tr>
<tr>
<td>Line of Business </td>
<td>&nbsp;</td>
<td><input name=”lofb” type=”text” id=”lofb” size=”25″></td>
</tr>
</table>
</div>
<p align=”left”>&nbsp;</p>
<p align=”left”> <span class=”style11″><span class=”body style7″><span class=”style10 body style16″><strong>Message:</strong></span></span></span><span class=”caption”><br>
</span>
<textarea rows=”10″ name=”message” cols=”30″></textarea>
<span class=”caption”> <br>
<br>
<input name=”Submit” type=”submit” id=”Submit” value=”Submit”>
&nbsp; &nbsp; &nbsp;
<input name=”reset” type=”reset” id=”reset” value=”Reset”>
</span></p>
</form>
<p></p>
</body>
</html>

[B]mailertestnewcareer.php code:[/B]

<?php

# #

$to = “[email protected]“; #set address to send form to
$subject = “Advertising Information; #set the subject line
$headers = “From: Form Mailer”; #set the from address
$message = “”; #set the from address
$forward = 1; # redirect? 1 : yes || 0 : no
$location = “http://www.teste.com/thankyou.html“; #set page to redirect to, if 1 is above

# #

$headers = “From: ” . $_POST[“name”];
$headers .= “<” . $_
POST[“email”] . “>rn”;
$headers .= “Reply-To: ” . $_POST[“email”] . “rn”;
$headers .= “Return-Path: ” . $_
POST[“email”];

$message .= “nn”;
$message .= “Email: ” . $_POST[“email”] . “rn”;
$message .= “Company Name: ” . $_
POST[“cname”] . “rn”;
$message .= “Name: ” . $_POST[“name”] . “rn”;
$message .= “Address: ” . $_
POST[“address”] . “rn”;
$message .= “City: ” . $_POST[“city”] . “rn”;
$message .= “State: ” . $_
POST[“state”] . “rn”;
$message .= “Zip Code: ” . $_POST[“zip”] . “rn”;
$message .= “Phone: ” . $_
POST[“phone”] . “rnn”;
$message .= “Line of Business: ” . $_POST[“lofb”] . “rnn”;
$message .= “Message: ” . $_
POST[“message”] . “rn”;

mail($to, $subject, $message, $headers);
if ($forward == 1) {
header (“Location:$location”);
}
else {
echo (“Thank you for submitting our form. We will get back to you as soon as possible.”);
}
?>

[upl-file uuid=64ab8a45-b756-4bfd-9d5a-907ba567ecf6 size=2kB]careerform.zip[/upl-file]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@hastxAug 02.2007 — step1 is to make sure your form will handle file uploads
[CODE]<form method="POST" action="mailertestnewAd.php" enctype="multipart/form-data">[/CODE]

step 2 is to add the file input to the form
[CODE]File to Upload: <input type="file" name="filename" />[/CODE]

step 3 would be to add the code to the top of your script to handle the upload
[code=php]
$filename = $_POST['filename'];
$uploaddir = 'myfolder/'; // where to upload the file to, if same folder as script use "./"
$file = basename($_FILES['filename']['name']);
$uploadfile = $uploaddir . $file;
echo '<pre>';
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.n";
} else {
echo "Problems! Maybe the file is too big. n";
}
echo '</pre>'
[/code]

this is a very simple example, an i havn't tested it. you would be better to look for a more robust script to handle your upload

After the upload works, you can decide if you want to attach it to an email
×

Success!

Help @LUTRELLA 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.18,
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,
)...