php form with upload

Soldato
Joined
25 Jan 2007
Posts
4,803
Location
King's Lynn
I've managed to get together a script (mostly program generated I must admit as I'm no php expert) that does do what I want kind of but threres a couple of bits which aren't doing as I would like or expect and just want to see if its an easy fix or the area I need to look into to get it working :).

1) It doesn't appear to keep the data on the page if you go to another page and then go back in the browser using the back button. Is this something to do with storing the session info?

2) It has to have an @ symbol used in the (I assume) email box and if it doesnt it either gives an error or tries to load the php file.
I think its down to it checking but I'm not worried over that, I'm not stupid when it comes to emails etc so I don't really need it, so I don't mind if the checking bit is removed but I want to make sure its just this bit
Code:
function ValidateEmail($email)
   {
      $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
      return preg_match($pattern, $email);
   }
before deleting it and it won't stop the rest working :)

3) is there a way to make the script send you to a page if the file is too big rather than just not sending it through as it appears to do at the moment.

4) Where would I find the php.ini, is it accessible by me or is it a server wide thing, as I would like to up my limit (which is supposedly done in this file) from 1meg which my host currently appears to be using to atleast 2megs, I can upload in excess of 3 megs on my nas but can only do 1meg via my host (I will most likely be moving anyways as they don't seem to like replying to emails :cool:).

I've attached the php as in code below.
The sections on the form are
Name: name
Reference: reference
Telephone: telephone
E-mail: email
Message: message (this is a text box)
File Upload: file
reset
and submit

Any help here would be greatly appreciated as I've checked w3c and it didn't seem to have anything that I thought would help here. I'm open to alternative methods as long as they're php too :)

Code:
<?php
   function ValidateEmail($email)
   {
      $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
      return preg_match($pattern, $email);
   }

   if($_SERVER['REQUEST_METHOD'] == 'POST')
   {
      $mailto = 'email address here';
      $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
      $subject = 'Online Form Submission';
      $message = 'Values submitted from web site form:';
      $success_url = '/test/success.html';
      $error_url = '/test/failure.html';
      $error = '';
      $eol = "\n";
      $max_filesize = isset($_POST['filesize']) ? $_POST['filesize'] * 2048 : 2048000;
      $boundary = md5(uniqid(time()));

      $header  = 'From: '.$mailfrom.$eol;
      $header .= 'Reply-To: '.$mailfrom.$eol;
      $header .= 'MIME-Version: 1.0'.$eol;
      $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
      $header .= 'X-Mailer: PHP v'.phpversion().$eol;
      if (!ValidateEmail($mailfrom))
      {
         $error .= "The specified email address is invalid!\n<br>";
      }

      if (!empty($error))
      {
         $errorcode = file_get_contents($error_url);
         $replace = "##error##";
         $errorcode = str_replace($replace, $error, $errorcode);
         echo $errorcode;
         exit;
      }

      $internalfields = array ("submit", "reset", "send", "captcha_code");
      $message .= $eol;
      foreach ($_POST as $key => $value)
      {
         if (!in_array(strtolower($key), $internalfields))
         {
            if (!is_array($value))
            {
               $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
            }
            else
            {
               $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
            }
         }
      }

      $body  = 'This is a multi-part message in MIME format.'.$eol.$eol;
      $body .= '--'.$boundary.$eol;
      $body .= 'Content-Type: text/plain; charset=iso-8859-1'.$eol;
      $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
      $body .= $eol.stripslashes($message).$eol;
      if (!empty($_FILES))
      {
          foreach ($_FILES as $key => $value)
          {
             if ($_FILES[$key]['error'] == 0 && $_FILES[$key]['size'] <= $max_filesize)
             {
                $body .= '--'.$boundary.$eol;
                $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
                $body .= 'Content-Transfer-Encoding: base64'.$eol;
                $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
                $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
             }
         }
      }
      $body .= '--'.$boundary.'--'.$eol;
      mail($mailto, $subject, $body, $header);
      header('Location: '.$success_url);
      exit;
   }
?>
 
Last edited:
Back
Top Bottom