0 Replies Latest reply on Jun 26, 2011 12:43 PM by pchinique

    php form with file attachment

    pchinique Level 1

      I'm trying to create a form with option to upload files, two options: jpg. eps or zip files. I would like these files to come through an attachment to my email.

       

      I can't get it to work. I've tried different methods and nothing works to get the files in any way.

       

      Any one knows how to do this? Here is the code

       

      PHP:

       

       

      <?php

       

      echo $savestring;

       

      //--------------------------Set these paramaters--------------------------

       

      // Subject of email sent to you.

      $subject = 'logo upload';

       

      // Your email address. This is where the form information will be sent.

      $emailadd = 'pchinique@yahoo.com';

       

      // Where to redirect after form is processed.

      $url = 'http://www.pchgraphics.com/magento/upload-completed-thankyou';

       

      // 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';

      $target_path = "http://www.pchgraphics/magento/upload/";

      $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

      $random_hash = md5(date('r', time()));

      //define the headers we want passed. Note that they are separated with \r\n

      $headers = "From: info@pchgraphics.com\r\nReply-To: info@pchgraphics.com";

      //add boundary string and mime type specification

      $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

      //read the atachment file contents into a string,

      //encode it with MIME base64,

      //and split it into smaller chunks

      $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));

      //define the body of the message.

      ob_start(); //Turn on output buffering

       

      // --------------------------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: '.$emailadd.'');

      echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

       

       

       

       

      if ((($_FILES["file"]["type"] == "image/jpg")

      || ($_FILES["file"]["type"] == "image/jpeg")

      || ($_FILES["file"]["type"] == "application/eps"))

      && ($_FILES["file"]["size"] < 20000))

        {

        if ($_FILES["file"]["error"] > 0)

          {

          echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

          }

        else

          {

          echo "Upload: " . $_FILES["file"]["name"] . "<br />";

          echo "Type: " . $_FILES["file"]["type"] . "<br />";

          echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

          echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

       

          if (file_exists("http://www.pchgraphics.com/magento/upload/" . $_FILES["file"]["name"]))

            {

            echo $_FILES["file"]["name"] . " already exists. ";

            }

          else

            {

            move_uploaded_file($_FILES["file"]["tmp_name"],

            "http://www.pchgraphics.com/magento/upload/" . $_FILES["file"]["name"]);

            echo "Stored in: " . "http://www.pchgraphics.com/magento/upload/" . $_FILES["file"]["name"];

            }

          }

        }

      else

        {

        echo "Invalid file";

        }

      ?>

       

       

       

      HTML:

       

       

       

      <form id="form1" action="http://www.pchgraphics.com/magento/php/file-upload2.php" enctype="multipart/form-data" method="post">

      <table border="0" cellspacing="15" cellpadding="10" width="350">

      <tbody>

      <tr>

      <td>

      <p>Name:<br /> <input id="name" name="name" type="text" /></p>

      </td>

      </tr>

      <tr>

      <td>

      <p>Last Name:           <br /> <input id="last-name" name="last-name" type="text" /></p>

      </td>

      </tr>

      <tr>

      <td>

      <p>E-mail:<br /><input id="email" name="email" type="text" /></p>

      </td>

      </tr>

      <tr>

      <td>

      <p>Price this Logo:<br /><input id="price" name="price" type="text" /></p>

      </td>

      </tr>

      <tr>

      <td>

      <p>Upload your .jpg           <br /> <input id="file-upload" name="file-upload" type="file" /></p>

      </td>

      </tr>

      <tr>

      <td>

      <p>Upload your .eps           <br /> <input id="file-upload2" name="file-upload2" type="file" /></p>

      </td>

      </tr>

      <tr>

      <td colspan="2" align="right"><input id="submit" name="submit" type="submit" value="Send" /></td>

      </tr>

      </tbody>

      </table>

      </form>