Support Forums

Full Version: php form script with upload script help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,

I had 2 separate scripts, but was wanting to combined them into 1, but I'm not exactly sure what I'm doing so any help would be appreciated

Code:
<?php

$company = $_POST["company"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$work = $_POST["work"];
$page = $_POST["page"];
$referral= $_POST["referral"];
$comments = $_POST["comments"];




$address_to = "removed";
$address_from = "From: test@test.com";
$email_subject_line = $company . "'s form";

$email_text = "Company Name?" . $company .
"\nPhone number? " . $phone .
"\nEmail Address? " . $email .
"\nType of work? " . $work .
"\nHow many pages? " . $page .
"\nReferral?" . $referral .
"\nComments? " . $comments;



mail($address_to, $email_subject_line, $email_text, $address_from);

// Configuration - Your Options
      $allowed_filetypes = array('jpg','gif','bmp','png','jpeg','zip',); // These will be the types of file that will pass the validation.
      $max_filesize = 3145728; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = './uploads'; // 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(false === in_array(pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION), $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.');

   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
         echo 'Your file upload was successful, go back to the homepage by clicking <a href="http://www.andrewshemo.com/test/form" title="">here</a>'; // It worked.
      else
         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.

?>

Code:
<html>
<head>
</head>
<body>
<form name="contact" enctype="multipart/form-data" action="contact.php" method="post" onsubmit="return validate_fields(this)">
<div id="form">
<ul>
<li>Name:<br />
<input type="text" size="30" name="name" id="name" /></li>
<li><br /></li>
<li>Location:<br />
<input type="text" name="location" id="location" /></li>
<li><br /></li>
<li>Make:<br />
<input type="text" name="make" id="make" /></li>
<li><br /></li>
<li>Model:<br />
<input type="text" name="model" id="model" /></li>
<li><br /></li>
<li>Referral:<br />
<input type="text" name="referral" id="referral" /></li>
<li><br /></li>
<li><label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
<button>Upload File</button></li>
<li><br /></li>
<li><input type="submit" name="submit" value="Submit" />
&nbsp;
<input type="reset" name="reset" value="Reset" /></li>
</ul>
</div>
<!-- End Contact Form -->
</form>
</body>
</html>

is it possible to remove the upload option and make it so that when a user submits the form it'll upload their photos? or have it send the photo in the email with the form so that it does not use server space?
Hey, I combined the script together:

Code:
<html>
<head>
</head>
<body>
<?php
if(! $_POST['submit'])
{
    ?>
<form name="contact" enctype="multipart/form-data" action="contact.php" method="post" onsubmit="return validate_fields(this)">
<div id="form">
<ul>
<li>Name:<br />
<input type="text" size="30" name="name" id="name" /></li>
<li><br /></li>
<li>Location:<br />
<input type="text" name="location" id="location" /></li>
<li><br /></li>
<li>Make:<br />
<input type="text" name="make" id="make" /></li>
<li><br /></li>
<li>Model:<br />
<input type="text" name="model" id="model" /></li>
<li><br /></li>
<li>Referral:<br />
<input type="text" name="referral" id="referral" /></li>
<li><br /></li>
<li><label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
<button>Upload File</button></li>
<li><br /></li>
<li><input type="submit" name="submit" value="Submit" />
&nbsp;
<input type="reset" name="reset" value="Reset" /></li>
</ul>
</div>
<!-- End Contact Form -->
</form>
    <?php
}
else
{
    $company = $_POST["company"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $work = $_POST["work"];
    $page = $_POST["page"];
    $referral= $_POST["referral"];
    $comments = $_POST["comments"];

    $address_to = "removed";
    $address_from = "From: test@test.com";
    $email_subject_line = $company . "'s form";

    $email_text = "Company Name?" . $company .
    "\nPhone number? " . $phone .
    "\nEmail Address? " . $email .
    "\nType of work? " . $work .
    "\nHow many pages? " . $page .
    "\nReferral?" . $referral .
    "\nComments? " . $comments;



    mail($address_to, $email_subject_line, $email_text, $address_from);

    // Configuration - Your Options
          $allowed_filetypes = array('jpg','gif','bmp','png','jpeg','zip',); // These will be the types of file that will pass the validation.
          $max_filesize = 3145728; // Maximum filesize in BYTES (currently 0.5MB).
          $upload_path = './uploads'; // 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(false === in_array(pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION), $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.');

       // Upload the file to your specified path.
       if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
             echo 'Your file upload was successful, go back to the homepage by clicking <a href="http://www.andrewshemo.com/test/form" title="">here</a>'; // It worked.
          else
             echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
}
?>
</body>
</html>

May I suggest that you take a look at PEAR's email class, It's great for sending email's with attachment's.

http://pear.php.net/packages.php?catpid=14&catname=Mail

Hope I helped you.
thanks. I'll test it out in a bit!

got it working..
No problem bro, glad I helped you.
You could use google docs to manage your forms and then no need for php just pure html.