I have a generic form on my website, but I’d like to add the option to upload a file with the form - ultimately sending the document to me as an attachment. I’ve found PHP codes online to try to get it to work, but I can’t figure it out. First, I guess I need to know if this is possible (which I’m pretty certain it is) and second, I need help with the code.
Here is the html code for the actual form:
<form action="sr.php" method="post" enctype="multipart/form-data" name="Name" target="_parent" id="Name">
<table width="722" border="0">
<tr>
<td width="193"> </td>
<td width="161"><div align="left" class="style9">Name</div></td>
<td width="199"><span id="sprytextfield5">
<label>
<input name="Name" type="text" id="Name" size="45">
</label>
<span class="textfieldRequiredMsg"></span></span></td>
<td width="151"> </td>
</tr>
<tr>
<td> </td>
<td><div align="left" class="style9">Company Name</div></td>
<td><span id="sprytextfield4">
<input name="Company" type="text" id="Company" size="45">
<span class="textfieldRequiredMsg"></span></span></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><div align="left" class="style9">Account Number</div></td>
<td><span id="sprytextfield3">
<input name="MID" type="text" class="style2" id="MID" size="54">
<span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><div align="left" class="style9">Email Address</div></td>
<td><span id="sprytextfield2">
<input name="Email" type="text" class="style2" id="Email" size="54">
<span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><div align="left" class="style9">Phone Number</div></td>
<td><span id="sprytextfield6">
<input name="Phone" type="text" class="style2" id="Phone" size="54">
<span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td>
<td> </td>
</tr>
<tr>
<td colspan="4"><div align="left"> <span id="sprycheckbox2">
<label></label>
</span>
<table width="100%" border="0" cellpadding="2">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<br>
</div></td>
</tr>
<tr>
<td colspan="4"><div align="left">
<span id="sprycheckbox1">
<label>
<span class="style1">
<span class="style2">
<input type="checkbox" name="Certify" id="Certify">
</span></span></label>
</span><span class="style9"> I declare that I have read and understand the guidelines and that I comply within these guidelines. </span>.<br>
</div></td>
</tr>
<tr>
<td colspan="4"><div align="right"><span class="style12">
<label>
*All Fields Are Required
<div align="center">
<input type="submit" name="Subject" value="Submit">
And here is my php code:
[php]<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'I am Compliant ';
// Your email address. This is where the form information will be sent.
$emailadd = ‘[email protected]’;
// Where to redirect after form is processed.
$url = ‘/confirmation.html’;
// Makes all fields required. If set to ‘1’ no field can not be empty. If set to ‘0’ any or all fields can be empty.
$req = ‘1’;
// --------------------------Do not edit below this line--------------------------
$text = “Results from form:\n\n”;
$space = ’ ';
$line = ’
‘;
foreach ($_POST as $key => $value)
{
if ($req == ‘1’)
{
if ($value == ‘’)
{echo “$key is empty”;die;}
}
$j = strlen($key);
if ($j >= 20)
{echo “Name of form element $key cannot be longer than 20 characters”;die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ’ ‘;}
$value = str_replace(’\n’, “$line”, $value);
$conc = “{$key}:$space{$value}$line”;
$text .= $conc;
$space = ’ ';
}
mail($emailadd, $subject, $text, ‘From: ‘[‘Email’]’’);
echo ‘’;
?>
[/php]
Oh…and here is the script that I found online for uploading, but not sure it is even what I actually need.
[code]
Select a file:
Upload File
[/code] ....and.... [php]<?php // Configuration - Your Options $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation. $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB). $upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $_FILES[‘userfile’][‘name’]; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,’.’), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die(‘The file you attempted to upload is not allowed.’);
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES[‘userfile’][‘tmp_name’]) > $max_filesize)
die(‘The file you attempted to upload is too large.’);
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die(‘You cannot upload to the specified directory, please CHMOD it to 777.’);
// We’ll start handling the upload in the next step
?>[/php]