Jump to content

Email php form - need to add ability to accept file uploads


wolfsta

Recommended Posts

Hi

 

Have got my form working for all standard bits.  Just need to add the functionality for people to upload photos.  I copied and modified the form from a site and there is a section that I think is redundant it still works when I comment it out can you clarify for me:

 


    // validation expected data exists
/*   if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     */

 

Below is the full code for the form:

 


<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "asdfasdf@asdfasdf.com";
    $email_subject = "adsfasdf asdfasdf Application Form";
     
     
    function died($error) {
        // your error code can go here
        ?>
            
		<title>asdfas asdfasd</title>
		<style type="text/css">
		body {
			background-color: #000;
			text-align: center;
		}
		body,td,th {
			color: #FFF;
			font-family: Arial, Helvetica, sans-serif;
			font-size: 24px;
		}
		</style>
		</head>
		<body link="#FFFFFF" onLoad="setTimeout('history.back()',10000)">
		<p><img src="../images/logo.png" width="326" height="144" alt="asdfasdf" longdesc="http://www.adsfasdf.com" /><br />
		</p>
            <br />
		<p>We are very sorry, but there were error(s) found with the form you submitted.</p>
          	
            <p>Please fix the following errors:</p>
            <hr />
            <br />
            <?php
		echo $error;
		?>
            
            <hr />
            <br />
            
		<p>Click back to fix your error(s) or you will be taken back to the form automatically in 10 seconds...</p>
		<h6> </h6>
		<h6>© asdfasdf 2012</h6>
		</body>
		</html>
            
            <?php
        die();
    }
     
    // validation expected data exists
/*   if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     */
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    $age = $_POST['age'];
$city = $_POST['city'];
$state = $_POST['state'];
$height_feet = $_POST['height_feet'];
    $height_inches = $_POST['height_inches'];


    $error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'First Name: This is not a valid name.<br /><br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'Last Name: This is not a valid last name.<br /><br />';
  }
    $age_exp = "/^(1[89]|[2-9][0-9])$/";
  if(!preg_match($age_exp,$age)) {
    $error_message .= 'Age:  You need to be at least 18+ to apply.<br /><br />';
  }
    $phone_exp = "/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/";
  if(!preg_match($phone_exp,$telephone)) {
    $error_message .= 'Phone: eg 646 555 1234 or 646-555-1234 or (646) 555 1234<br /><br />';
  }
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'Email: Your address is not invalid eg yourname@emaildomain.com<br /><br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'Comments: Please leave a breif message explaining your interest and if you have any previous experience etc.<br /><br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below:\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name:"."\t".clean_string($first_name)."\n";
    $email_message .= "Last Name:"."\t".clean_string($last_name)."\n";
$email_message .= "Age:"."\t".clean_string($age)."\n";
$email_message .= "Height:"."\t".clean_string($height_feet).'ft ' . clean_string($height_inches).'in'."\n";
$email_message .= "City:"."\t".clean_string($city)."\n";
$email_message .= "State:"."\t".clean_string($state)."\n";
    $email_message .= "Email:"."\t".clean_string($email_from)."\n";
    $email_message .= "Telephone:"."\t".clean_string($telephone)."\n";
    $email_message .= "Comments:"."\t".clean_string($comments)."\n";

     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

<title>adsfas asdfasdf</title>
<style type="text/css">
body {
background-color: #000;
text-align: center;
}
body,td,th {
color: #FFF;
font-family: Arial, Helvetica, sans-serif;
font-size: 24px;
}
</style>
</head>

<body link="#FFFFFF" onLoad="setTimeout('history.back()',10000)">
<p><img src="../images/logo.png" width="326" height="144" alt="asdfasdf" longdesc="http://www.asdfasdf.com" /><br />
</p>
<p>Thank your for applying to asdf adsf.  We will be in touch with you very soon.</p>
<p>You will be redirected back to the site in 10 seconds...</p>
<h6> </h6>
<h6>© adsfdasf 2012</h6>
</body>
</html>

<?php
}
?>

 

 

Here is the html form:

 


<form id="form2" action="./php/joinus.php" method="post" enctype="multipart/form-data">
					<fieldset>
						<div class="wrapper">
							<div class="fleft col">
								<label class="first_name">
									<span class="text-form">Name:</span>
									<input name="first_name" type="text" value="">
								</label>
                                    <label class="age">
									<span class="text-form">Age:</span>
									<input name="age" type="tel" value="">
								</label>
								<label class="city">
									<span class="text-form">City:</span>
									<input name="city" type="text" value="">
								</label>
                                    <label class="telephone">
									<span class="text-form">Phone:</span>
									<input name="telephone" type="tel" value="">
								</label>

							</div>
							<div class="fleft col2">
								<label class="last_name">
									<span class="text-form">Last Name:</span>
									<input name="last_name" type="text" value="">
								</label>
                                    
                                    <span id="validate_height">
			<label><span class="text-form">Height:</span>
				<select style="width:109px;" name="height_feet">
					<option value="" selected="selected">Feet</option>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
					<option value="4">4</option>
					<option value="5">5</option>
					<option value="6">6</option>
					<option value="7">7</option>
					<option value="8">8</option>
				</select>
				<select style="width:109px;" name="height_inches">
					<option value="" selected="selected">Inches</option>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
					<option value="4">4</option>
					<option value="5">5</option>
					<option value="6">6</option>
					<option value="7">7</option>
					<option value="8">8</option>
					<option value="9">9</option>
					<option value="10">10</option>
					<option value="11">11</option>
				</select>
			</label>
			</span>

			<label class="state">
                	<span class="text-form">State:</span> 
				<select name="state">
					<option value="" selected="selected">Select a State</option>
					<option value="Alabama">Alabama</option>
					<option value="Alaska">Alaska</option>
					<option value="Arizona">Arizona</option>
					<option value="Arkansas">Arkansas</option>
					<option value="California">California</option>
					<option value="Colorado">Colorado</option>
					<option value="Connecticut">Connecticut</option>
					<option value="Delaware">Delaware</option>
					<option value="District Of Columbia">District Of Columbia</option>
					<option value="Florida">Florida</option>
					<option value="Georgia">Georgia</option>
					<option value="Hawaii">Hawaii</option>
					<option value="Idaho">Idaho</option>
					<option value="Illinois">Illinois</option>
					<option value="Indiana">Indiana</option>
					<option value="Iowa">Iowa</option>
					<option value="Kansas">Kansas</option>
					<option value="Kentucky">Kentucky</option>
					<option value="Louisiana">Louisiana</option>
					<option value="Maine">Maine</option>
					<option value="Maryland">Maryland</option>
					<option value="Massachusetts">Massachusetts</option>
					<option value="Michigan">Michigan</option>
					<option value="Minnesota">Minnesota</option>
					<option value="Mississippi">Mississippi</option>
					<option value="Missouri">Missouri</option>
					<option value="Montana">Montana</option>
					<option value="Nebraska">Nebraska</option>
					<option value="Nevada">Nevada</option>
					<option value="New Hampshire">New Hampshire</option>
					<option value="New Jersey">New Jersey</option>
					<option value="New Mexico">New Mexico</option>
					<option value="New York">New York</option>
					<option value="North Carolina">North Carolina</option>
					<option value="North Dakota">North Dakota</option>
					<option value="Ohio">Ohio</option>
					<option value="Oklahoma">Oklahoma</option>
					<option value="Oregon">Oregon</option>
					<option value="Pennsylvania">Pennsylvania</option>
					<option value="Rhode Island">Rhode Island</option>
					<option value="South Carolina">South Carolina</option>
					<option value="South Dakota">South Dakota</option>
					<option value="Tennessee">Tennessee</option>
					<option value="Texas">Texas</option>
					<option value="Utah">Utah</option>
					<option value="Vermont">Vermont</option>
					<option value="Virginia">Virginia</option>
					<option value="Washington">Washington</option>
					<option value="West Virginia">West Virginia</option>
					<option value="Wisconsin">Wisconsin</option>
					<option value="Wyoming">Wyoming</option>
				</select>
			</label>
                
                
                <label class="email">
									<span class="text-form">E-mail:</span>
									<input name="email" type="text" value="">
								</label>
                                    


							</div>
						</div>
						<label class="message">
							<span class="text-form">Comments:</span>
							<textarea name="comments"></textarea>
						</label>
						<label>
							<span class="text-form2">Attach a photo:</span>
							<input id="photo" name="file[]" type="file" class="fl" >
						</label>
						<label>
							<span class="text-form2">Attach a photo:</span>
							<input id="photo" name="file[]" type="file" class="fl"  >
						</label>
                            <label>
							<span class="text-form2">Attach a photo:</span>
							<input id="photo" name="file[]" type="file" class="fl"  >
						</label>
							<div class="but"><a class="link1 link-color2" data-type="submit" onClick="document.getElementById('form2').submit()">Submit</a></div>
					</fieldset>
				</form>

 

From what I've been reading you need to tell the php to put it to a temporary file from the submit.. then move it to the file name and then somehow include that file name in the php email back.  So maybe a url and its private or htaccess? or does it get included as attachment in the mail?

 

Im not sure if i should be using 'id' or 'name' in the html form etc and how to tie it together.

 

Any help or guides much appreciated.

 

Thanks

 

Wolfsta

Link to comment
Share on other sites

So I found this code and it seems to go through the upload process in the browser.. but it doesn't copy it to my upload folder..

 


//Get the uploaded file information
$name_of_uploaded_file =
    basename($_FILES['uploaded_file']['name']);
//get the file extension of the file
$type_of_uploaded_file =
    substr($name_of_uploaded_file,
    strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file =
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs

//Settings
$max_allowed_file_size = 500000; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
  $errors .= "\n Size of file should be less than $max_allowed_file_size";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
  if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
  {
    $allowed_ext = true;
  }
}
if(!$allowed_ext)
{
  $errors .= "\n The uploaded file is not supported file type. ".
  " Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//copy the temp. uploaded file to uploads folder
$upload_folder = "./upload/";
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
}


 

If it makes a difference I inserted that into the previous posts php just after this section:

 


  $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    $age = $_POST['age'];
$city = $_POST['city'];
$state = $_POST['state'];
$height_feet = $_POST['height_feet'];
    $height_inches = $_POST['height_inches'];

Link to comment
Share on other sites

Solved that part.. was the folder permissions changed to ../uploads/ and chmod to 777 and it now uploads the file. 

 

Can anyone tell me how I can now get it to send it with the email as an attachment then remove the file from server.

 

Thanks

 

Wolfsta

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.