HELP - Dummy file attached when the field left blank/empty

Hello,

Below is email attachment code, that works great with attachment. But the problem is that if I leave the attachment field blank, it attachs a dummy (empty) file. I have not figure out why, I tried so many thing and none worked. Some PLEASE help…

[php]

Information <?php $turnaround = "1 business day"; $accounts = array( "Account 1", "Account 2", );

$platforms = array( “Mainframe”, “LUW” );
$types = array( “Service Request / Project”, “Consultation / Information Request” );
$priorities = array( “Low”, “Medium”, “High” );

//if ( $_SERVER[‘REQUEST_METHOD’] == “POST” )
if(isset($_FILES) && (bool) $_FILES)
{

// define allowed extensions
$allowedExtensions = array(" ",“pdf”,“doc”,“docx”,“gif”,“jpeg”,“jpg”,“png”,“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'];
     
     // 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");
     //}

     // move this file to the server YOU HAVE TO DO THIS
     $server_file = "/tmp/$path_parts[basename]";
     move_uploaded_file($temp_name,$server_file);
     
     // add this file to the array of files
     array_push($files,$server_file);
  }  
  $subject = join(" - ", array ($_POST['priority'], $_POST['startdate'], $_POST['enddate'], $_POST['platform'], $_POST['type'], $_POST['account'], $_POST['abstract']));
  $msg =  join (array ("*** This message was sent from an automated service ID.  Please do not respond to this message. ***\n\nSYSTEMS\n-------\n" . $_POST['systems'] . "\n\nDBS\n---\n" . $_POST['dbs'] . "\n\nINFO\n----\n" . $_POST['info'] . "\n\nAttachment\n-------\n" . "\n"));
  $headers = join( "\r\n", array( "Reply-To: " . $_POST['email'], "From: [email protected]", "Cc: " . $_POST['email'] ) );

  // 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) {
     $aFile = fopen($file,"rb");
     $data = fread($aFile,filesize($file));
     fclose($aFile);
     $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";

}

//now we just send the message
if ( $_POST[‘platform’] == “Mainframe” )
{
mail("[email protected]", $subject, $message, $headers);
}
else
{
mail("[email protected]", $subject, $message, $headers);
}

?>

Your request has been submitted and you will receive a response within <?= $turnaround ?>.

[Back]

<?php } else { ?>

Complete the following form to request service from support.


All fields marked with * are mandatory.

Request Abstract*
Email Address*
(e.g. [email protected])
Request Type* <?php foreach ( $types as $type ) { ?> <?= $type ?> <?php } ?>
Account* <?php foreach ( $accounts as $account ) { ?> <?= $account ?> <?php } ?>
Platform* <?php foreach ( $platforms as $platform ) { ?> <?= $platform ?> <?php } ?>
Requested Start Date* ">
Requested Completion Date* ">
Priority* <?php foreach ( $priorities as $priority ) { ?> <?= $priority ?> <?php } ?>
Subsystem Name (zOS) or Server Names (LUW)
DATABASE Name(s)
Additional Information

Attachment:



<?php } ?> [/php]

Well, down where you are creating the message and you start adding the File-Attachments, you need to check the count of the files. Something like if(count($files)>0) {Do the sends} …

In other words, BEFORE you send any files, you need to see if there are any… Just put an IF around them counting the files first…

Hope that helps…

Hi

I triedd as you suggested, but still does not work. Below is section if put the “if count” statement

[php]
if ( count($files)>0 ) {
// part 2: loop and define mail attachments
print_r($files);
foreach($files as $file) {
$aFile = fopen($file,“rb”);
$data = fread($aFile,filesize($file));
fclose($aFile);
$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 .= “boundary=–{$mime_boundary}–\n”;
}

[/php]

Also, I did a print_r($files), below is output:

  1. Attached file out ==> Array ( [0] => /tmp/error.txt )

  2. Wihtout Attachment ==> Array ( [0] => /tmp/ )

Any other suggestion I can try?

Hello,

Never mind, I was able solve the problem. I added following if statement and work great.

[php]
if ( !empty($path_parts[basename]) ) {

[/php]

Great! I was just writing you a solution… Glad you found it…

Sponsor our Newsletter | Privacy Policy | Terms of Service