PLEASE HELP! I am totally at a loss here! I am new to PHP coding. I have a client that is requesting the ability to be able to send an attachment through an email form. I had this working on my website (through hosting company A) but on the clients website (hosting company 😎 it does not work. I get the data back form the basic form fields but not the attachment. We went through the process of turning register globals on (they are off) but that didn't resolve the problem. Is there any reason why this would work on one server and not another?

Below is my php code:

// Obtain file upload vars
$attachment_name = $FILES['fileatt']['name'];
$attachment = $
FILES['fileatt']['tmp_name'];
$attachment_type = $FILES['fileatt']['type'];
$attachment_size = $
FILES['fileatt']['size'];

$headers = "From: $from";

if (is_uploaded_file($attachment)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($attachment,'rb');
$data = fread($file,filesize($attachment));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$attachment_type};\n" .
" name=\"{$attachment_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

My HTML code is for the attachment field is:

                              <td><div align="right">Resume:</div></td>
                              <td><input type="file" name="fileatt" size="40"> 

Any help would greatly be appreciated as I have no clue what do anymore. I have exhausted all my options over the past month trying to figure this out.

    I would start the debugging process by putting the following somewhere in the form processing file to confirm whether or not you are even receiving the file upload:

    printf("<pre>%s</pre>\n", print_r($_FILES, 1));
    

    If not, have you included the enctype="multipart/form-data" attribute in your <form> tag?

      I get an empty array when I put your code in.

      Here is my form tag:

      		<form 
      			name="employment" 
      			action="employment.php" 
      			method="post" 
      			enctype="multipart/form-data"				
      			>

      I got the hosting company to put error reporting on for me...I get the following when I try and process the form:

      Notice: Undefined index: fileatt in /var/www/vhosts/abc.com/httpdocs/employment.php on line 36

      Notice: Undefined index: fileatt in /var/www/vhosts/abc.com/httpdocs/employment.php on line 37

      Notice: Undefined index: fileatt in /var/www/vhosts/abc.com/httpdocs/employment.php on line 38

      Notice: Undefined index: fileatt in /var/www/vhosts/abc.com/httpdocs/employment.php on line 39

        abkdesign wrote:

        I get an empty array when I put your code in.

        That would seem to indicate that no file is being uploaded from your form page. Are $_POST values being received?

        You might want to run the form page through the W3C HTML Validator to make sure it is properly formed. If there are no HTML errors, then you might want to provide a link to it here so we can take a look (or copy the HTML here in a reply).

          I ran the code through the W3C HTML Validator and there were no errors.

          I am getting back all other $POST data just not the $FILES.

          Here is my HTML code for the FORM:

          			<form 
          				name="employment" 
          				action="employment.php" 
          				method="post" 
          				enctype="multipart/form-data"				
          				>
          				<!--onsubmit="return validateEmploymentForm(employment)"-->
          				<input name="request_attempt" type="hidden" value="true">		
          
          			<table border="0" align="center"><tr><td>
          			  <div style="border: solid 2px #1D1651; padding: 2px 2px 2px 2px;" align="center">                   
                                    <table width="600" border="0" cellspacing="5" cellpadding="0" align="center">
          						  <?php if (isset($_POST["request_attempt"]) != "true") { ?>
                                      <tr class="dkbluetxtNotBold"> 
                                        <td colspan="2" class="dkbluetxt"><div align="center">Are 
                                            you interested in applying for a position 
                                            at <br/>
                                            Rockwood Title &amp; Settlement Group? Submit 
                                            your resume below.<br/>
                                            <br/>
                                          </div></td>
                                      </tr>
                                      <tr class="dkbluetxtNotBold"> 
                                        <td width="190" class="dkbluetxt"><div align="right">Name: 
                                          </div></td>
                                        <td width="395"> <input name="name" type="text" size="50"></td>
                                      </tr>
                                      <tr class="dkbluetxtNotBold"> 
                                        <td height="24" class="dkbluetxt">
          <div align="right">Email: 
                                              </div></td>
                                            <td> <input name="email" type="text" size="50"></td>
                                          </tr>
                                          <tr class="dkbluetxtNotBold"> 
                                            <td class="dkbluetxt"><div align="right">Position 
                                                Applying for: </div></td>
                                            <td> <input name="position" type="text" size="50"></td>
                                          </tr>
                                          <tr class="dkbluetxtNotBold"> 
                                            <td class="dkbluetxt"><div align="right">Resume:</div></td>
                                            <td><input type="file" name="fileatt" size="40"> 
                                            </td>
                                          </tr>
                                          <tr class="dkbluetxtNotBold"> 
                                            <td valign="top" class="dkbluetxt">&nbsp;</td>
                                            <td> <div align="right"> 
                                                <input type="submit" name="Submit" value="Submit">
                                                &nbsp; 
                                                <input type="reset" name="Reset" value="Reset">
                                              </div></td>
                                          </tr>
                                          <?php } else { ?>
          				<tr>
          					              <td colspan="5" class="header"> 
                                              <?php if($isEmailSuccessful == true) { ?>
                                              <p class="dkbluetxt" align="center">Thank 
                                                You.<br>
                                                <br>
                                                Your resume has been submitted successfully.</p>
          						            <?php } else { ?>
                                              <p class="dkbluetxt" align="center">An error 
                                                has occured while attempting to submit your 
                                                resume. <br>
                                                <br>
                                                Please <a href="mailto:mcunningham@rockwoodtitle.com">email</a> 
                                                us directly or fax your resume to 410-872-9085.</p>
                                        <?php } ?>
                                      </td>
          				</tr>
                                          <?php } ?>
                                        </table>
          				  </div>
          				  </td></tr></table>
                            </form>
          

            As I mentioned in my first post this works on one hosting company but not the one that I really need it to work. I don't get it?

              Perhaps the host in question does not allow file uploads? Check the file_uploads setting.

                Any other suggestions? I am really stuck and need to get this working for my client asap! Thanks!

                  One reason a script may not work on one server and works well on another is different software. Perhaps your client is using an older version of PHP?

                  In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES. Judging by your phpinfo() scripts, that doesn't seem so. But it's worth looking at!

                  Maybe you're just having an issue w/the upload itself? I don't know for sure. I wrote an image uploader test script a while back & it's helped others, maybe it can help you. It's a small, 2 part script.

                  <?php
                  # vars.php
                  /*
                   *  Maximum Size an image may be
                   *
                   *  $maxImageSize
                   *
                   *  51200 = 50 KB
                   *  512000 = 500 KB
                   *
                   */
                  
                  $maxImageSize = "512000"; 
                  
                  
                  
                  /*
                   *  Path Information
                   *
                   *  1.) $file_dir
                   *
                   *  2.)$file_url
                   *
                   *  1.) full path ex /home/user/public_html/images
                   *
                   *  2.) full url ex http://www.site.com/images
                   *
                   *  NO TRAILING SLASH!!
                   * Directory to move images to should be writeable. If you must,  chmod 777
                   *
                   */
                  
                  $file_dir = "/home/paul/public_html/FREE/upload/images";
                  $file_url = "http://localhost/~paul/FREE/upload/images";
                  
                  /*
                   *  Defining accepted Image Types
                   *
                   *  Add more like shown below: 
                   *
                   *  $acceptedImageType[] = "type";
                   *
                   */
                  
                  
                  $acceptedImageType = array ( "pjpeg", "pjpg", "jpg", "jpeg", "gif", "png", "x-png", "PNG", "x-xbitmap" );
                  $acceptedImageType[] = "x-bmp";
                  
                  /*
                   *  Defining a Page's Own URL
                   *
                   *  $self=$_SERVER['PHP_SELF'];
                   *
                   */
                  
                  $self=$_SERVER['PHP_SELF'];
                  ?>
                  
                  <?php
                  # upLoadForm.php
                  
                  include_once("vars.php");
                  // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
                  // of $_FILES.
                  
                  # Foreach loop 1
                  foreach( $_FILES as $file_name => $file_array )
                  {
                  echo "name: ".$file_array['name']."<br />\n";
                  echo "type: ".$file_array['type']."<br />\n";
                  echo "tmp_name: ".$file_array['tmp_name']."<br />\n";
                  echo "error: ".$file_array['error']."<br />\n";
                  echo "size: ".$file_array['size']."<br />\n";
                  // print_r($_FILES);
                  
                  $tmp_name = $file_array['tmp_name'];
                  
                  if( strstr( $tmp_name, "/tmp/" ) ) {
                     $tmp_name = str_replace("/tmp/", "", $tmp_name); # strip location from name :Linux
                  }
                  
                  elseif( strstr( $tmp_name, "C:\\WINDOWS\\TEMP\\" ) ) {
                   $tmp_name = str_replace("C:\\WINDOWS\\TEMP\\", "", $tmp_name); # strip location from name :Windows
                   }  
                  # Foreach loop 2
                  foreach ( $acceptedImageType as $acceptThis ) {
                  if(is_uploaded_file( $file_array['tmp_name'] ) && $file_array['type'] == "image/$acceptThis" ) {
                  
                  $size=$file_array['size'];
                  if($size > $maxImageSize)
                  {
                  exit("The image is too large in size.");
                  }
                  
                  move_uploaded_file( $file_array['tmp_name'], "$file_dir/".$tmp_name)
                  or die("Couldn't Copy");
                  echo "<img src=\"$file_url/".$tmp_name."\">";
                  echo "<br />\n\n";
                  
                  } // End IF statement
                  
                  } // End Foreach loop 2
                  
                  } // End Foreach loop 1
                  
                  ?>
                  
                  <form action="<?php echo "$self"; ?>" enctype="multipart/form-data" method="POST">
                  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo "$maxImageSize"; ?>">
                  <input type="file" name="fupload">
                  <br /><br />
                  <input type="submit" value="Upload">
                  <br />
                  </form>
                  

                  I know this won't directly answer your question, but you did mention that you were new at working with PHP - if you understand the $file_array - working with files just gets easier.

                    DeadlySin3 - I tried your code and I can't get it to work. I modified the path to match my site. Should the file go in the $file_dir?

                      Write a Reply...