I am writing a submission form that sends a file attachment. It works, and sends the file. The problem I am having is that if no file is attached, it send’s a blank dummy file with no extension called upload. I have tried various checks to make sure a file is uploaded, and if not, it should skip the part creating the array to store the file into, but apparently it does not. I have used isset and !empty(var) but it still sends the dummy file. Can anyone please help?!
Here is the PHP
[php]<?php
//CREATING MAIL VARIABLES FROM FORM
$firstName = $_POST[‘firstName’];
$lastName = $_POST[‘lastName’];
$contactNumber1 = $_POST[‘contactNumber1’];
$contactNumber2 = $_POST[‘contactNumber2’];
$Hardware = $_POST[‘Hardware’];
$Software = $_POST[‘Software’];
$Details = $_POST[‘Details’];
$Publication = $_POST[‘Publication’];
$Revenue = $_POST[‘Revenue’];
$email_sender = "[email protected]";
if(!empty($_FILES)) {// did files get sent
$files = array();
// loop through all the files
foreach($_FILES as $name=>$file) {//for each
// define variables for temp
$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'];
// move this file to the server YOU HAVE TO DO THIS
$server_file = "upload/$path_parts[basename]";
move_uploaded_file($temp_name,$server_file);
// add this file to the array of files
array_push($files,$server_file);
}//end for each
}//end did files get sent
//create email variables
$to = "[email protected]";
$from = $email_sender;
$subject = “Message from Help Desk Submission Form”;
$msg = "
Name: $firstName $lastName\n
Contact Number/s: $contactNumber1 $contactNumber2\n
Problem Hardware: $Hardware\n
Problem Software: $Software\n
Affects Paper Publication: $Publication \n
Affects Company Revenue: $Revenue \n
Details about problem: $Details\n";
$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) {
$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”;
}
//SEND EMAIL
$ok = mail($to,$subject,$message,$headers);
if ($ok) {
echo
“
Message sent successfully to $to! We will email you as soon as possible.
”;unlink($server_file);
} else {
echo “
Message could not be sent!
”;}
die();
?>
[/php]
Here is the HTML Form
[code]
Help Desk Submission Form label{ width: 150px; display: inline-block; } label.error{ color: red; }<div id="header" class="container">
</div>
</div>
<!-- end #header -->
<div id="page" class="container">
Help Desk Support Form |
|
Please fill in all fields marked with * | |
First Name: * | |
Last Name: * | |
Department: * | Please Select Administration Advertising Customer Service Circulation Classifieds Human Resources Interactive Imaging Information Technology Maintenance Production Security Triad Cars Weekly |
Primary Contact Number: * | |
Optional Contact Number | |
Hardware having the problem: * | Please Select Desktop Laptop Server Printer Other Device |
Software having the problem: * | Please Select Access Adobe Flash Adobe InCopy Adobe InDesign Adobe Photoshop BDS Black Magic Cisco VPN Citrix DTI Excel Firefox Internet Explorer PBS Outlook Omnizone Planet Press Word Other Software |
Detailed information about your issue or request: * |
|
Will this issue cause todays paper not to publish? * | Yes No I don't Know |
Will this issue significantly impact revenue? * | Yes No I don't Know |
Additional attachments: | |
**Please keep in mind failure to do provide complete and accurate information could cause a delay in your request**
<!-- end #page -->
</div>
</div>