Jump to content

File Attachment Code - Please help!


bmccarthy

Recommended Posts

I can't figure out what the heck is wrong with this code. When I attempt to use it, it get an error message from my browser that the file is under maintenance or configured incorrectly.

I am attempting to have a user complete a form field on my website, upload a document, and send the email to me with the file attached.

 

<?php

$subject = 'I am from '.$_POST['MID'].' ';  
$emailadd = 'me@domain.com'; 
$req = '1'; 

$text = "I declare that I have read and understand the Guidelines and that I comply within these guidelines.  
:\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 = ' ';
}

$file_name = $_FILES['userfile'];
$new_file_name = $file_name . trim($_POST['MID']);
$path= "Confirmation/" . $new_file_name;
if($userfile) {
   if(copy($_FILES['userfile']['tmp_name'], $path)) {
     echo "Successful<BR/>";
     echo "File Name :" . $new_file_name."<BR/>";
     echo "File Size :" . $_FILES['userfile']['size']."<BR/>";
     echo "File Type :" . $_FILES['userfile']['type']."<BR/>";
   } else {
     echo "Error, file did not upload correctly!";
   }

mail($emailadd, $subject, $text, 'From: '.$_POST['Name'].' <'.$_POST['Email'].'>');

header("Location: compliance/confirmation.html");

?>

 

What am I doing wrong? Thanks!

Link to comment
Share on other sites

@batwimp

I replaced copy() with move_uploaded_file(), but still receive the configuration error message.

 

@xyph

$userfile is the key/name within the my html form on the webpage.

The html code for the form is:

<form action="sendresults.php" method="post" enctype="multipart/form-data" name="PCI" target="_parent" id="PCI">
                        <table width="722" border="0">
                          <tr>
                            <td width="193"> </td>
                            <td width="161"><div align="left" class="style9">Name</div></td>
                            <td width="199"><span id="sprytextfield5">
                              <label>
                              <input name="Name" type="text" id="Name" size="45">
                              </label>
                              <span class="textfieldRequiredMsg"></span></span></td>
                            <td width="151"> </td>
                          </tr>
                          <tr>
                            <td> </td>
                            <td><div align="left" class="style9">Company Name</div></td>
                            <td><span id="sprytextfield4">
                              <input name="Company" type="text" id="Company" size="45">
                              <span class="textfieldRequiredMsg"></span></span></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td> </td>
                            <td><div align="left" class="style9">ID Number</div></td>
                            <td><span id="sprytextfield3">
                              <input name="MID" type="text" class="style2" id="MID" size="54">
                              <span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td> </td>
                            <td><div align="left" class="style9">Email Address</div></td>
                            <td><span id="sprytextfield2">
                            <input name="Email" type="text" class="style2" id="Email" size="54">
                            <span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td> </td>
                            <td><div align="left" class="style9">Phone Number</div></td>
                            <td><span id="sprytextfield6">
                            <input name="Phone" type="text" class="style2" id="Phone" size="54">
                            <span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg"></span></span></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td colspan="4"><div align="left"> <span id="sprycheckbox2">
                                <label></label>
                            </span>
                                <table width="100%" border="0" cellpadding="2">
                                  <tr>
                                    <td><label for="userfile">
                                      <div align="center">Select a file:
                                    </label> 
                                      <input type="file" name="userfile"> 
                                      <br />
                                       </div></td>
                                    </tr>
                                </table>
                                <br>
                            </div></td>
                          </tr>
                          <tr>
                            <td colspan="4"><div align="left">
                              <span id="sprycheckbox1">
                              <label>
                              <span class="style1">
                              <span class="style2">
                              <input type="checkbox" name="Certify" id="Certify">
                              </span></span></label>
                              </span><span class="style9">                            I declare that I have read and understand the Security Guidelines and that I comply within these guidelines. I am Compliant</span>.<br>
                            </div></td>
                            </tr>
                          <tr>
                            <td colspan="4"><div align="right"><span class="style12">
                              <label>
                              *All Fields Are Required
                              <div align="center">
                                <input type="submit" name="I am Compliant" value="Submit">
                              </div>
                            </label></td>
                            </tr>
                        </table>
                                            </form>

Also, where in my .php do I put the error checker? I tried placing it after the mail command, but...?

Link to comment
Share on other sites

It could, but he's using the proper $_POST/$_FILES superglobal elsewhere. A 'better' assumption would be that he's using an undefined variable.

 

Or he could be using a version of PHP older than 4.2.

Link to comment
Share on other sites

@xyph

I apologize if I came across as dismissive. That was not my intention. I am a rookie at php so I don't necessarily know what I'm talking about. :)

 

Where in the code do I place the error checker? I tried placing it just below the mail command, but that didn't work. Where is the proper placement of it?

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.