Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
php form script with upload script help
#1
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?
Reply
#2
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.
Reply
#3
thanks. I'll test it out in a bit!

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 818 07-27-2020, 11:26 PM
Last Post: tk-hassan
  [help] Improve login script Montana" 1 1,579 03-18-2013, 12:59 PM
Last Post: Haxalot
  Req. Upload PHP script. 3 X P L 0 I T 1 1,239 07-14-2012, 05:19 PM
Last Post: 'Snorlax
  PHP email input form - having trouble getting the form to work... abayindi 4 2,533 03-19-2012, 10:02 AM
Last Post: RainbowDashFTW
  [NEED HELP] PHP Form post, get, echo Đενɨаηсε™ 5 1,899 02-06-2012, 01:16 PM
Last Post: ★Cooldude★

Forum Jump:


Users browsing this thread: 2 Guest(s)