Uploaded File empty in email - no errors

the mail() function

Moderators: macek, egami, gesf

austinmra
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Mar 12, 2012 1:02 pm

Uploaded File empty in email - no errors

Postby austinmra » Mon Mar 12, 2012 1:24 pm

I am using the following code to upload a file from the user and have it emailed to me. I receive no error messages but the file is not being uploaded to the server and it appears as an unnamed file in the email attachment. If anyone could have a look and offer a suggestion I would really appreciate it!

Thanks!

Code: Select all

<?php
   // did files get sent
   if(isset($_FILES) && (bool) $_FILES) {
   
      // define allowed extensions
      $allowedExtensions = array("pdf","doc","docx","rtf","txt");
      $files = array();
         
      // loop through all the files
      foreach($_FILES as $name=>$file) {
     
         // define some variables
         $file_name = $file['name'];
         $temp_name = $file['tmp_name'];
       $upload_folder = '/resumes/'; //<-- this folder must be writeable by the script
       $max_allowed_file_size = 100; // size in KB
         
         // check if this file type is allowed
         $path_parts = pathinfo($file_name);
         $ext = $path_parts['extension'];
         if(!in_array($ext,$allowedExtensions)) {
            die("extension not allowed");
         }
         
          //Get the uploaded file information
         $name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);
         
         //get the file extension of the file
         $type_of_uploaded_file = substr($name_of_uploaded_file,
                     strrpos($name_of_uploaded_file, '.') + 1);
                     
         $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;
      
      //copy the temp. uploaded file to uploads folder
      $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
      $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
      
      if(is_uploaded_file($tmp_path))
      {
          if(!copy($tmp_path,$path_of_uploaded_file))
          {
             $errors .= '\n error while copying the uploaded file';
          }
      }
         
         // add this file to the array of files
         array_push($files,$server_file);
      } 
         
      // define some mail variables
      $to = "info@mywebsite.ca";
      $from = "info@mywebsite.ca";
      $subject ="Resume Submission";
      $msg = "Please accept my application";
      $headers = "From: $from";
     
      // define our boundary
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
     
      // tell the header about the boundary
      $headers .= "\nMIME-Version: 1.0\n";
      $headers .= "Content-Type: multipart/mixed;\n";
      $headers .= " boundary=\"{$mime_boundary}\"";
     
      // part 1: define the plain text email
      $message ="\n\n--{$mime_boundary}\n";
      $message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
      $message .="Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
      $message .= "--{$mime_boundary}\n";
     
      // part 2: loop and define mail attachments
      foreach($files as $file) {
         
         $data = chunk_split(base64_encode($data));
         $message .= "Content-Type: {\"application/octet-stream\"};\n";
         $message .= " name=\"$file\"\n";
         $message .= "Content-Disposition: attachment;\n";
         $message .= " filename=\"$file\"\n";
         $message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
         $message .= "--{$mime_boundary}\n";
      }
        //------ Validate the file extension -----
   $allowed_ext = false;
   for($i=0; $i<sizeof($allowed_extensions); $i++)
   {
      if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
      {
         $allowed_ext = true;      
      }
   }   
      // send the email
      $ok = mail($to, $subject, $message, $headers);
      if ($ok) {
         echo "<p>mail sent to $to!</p>";
      } else {
         echo "<p>mail could not be sent!</p>";
      }
      die();
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
      <form method="post" action="/formemail.php" enctype="multipart/form-data">
         <input type="file" name="attach2"/>
         <input type="submit" value="submit"/>
   </form>
   </body>

</html>

Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest