Formular ohne Anhang versenden

Quu

Grünschnabel
#1
Hallo zusammen
Ich habe mir ein Formular gebaut, welches Attachments erlaubt.
Nun habe ich jedoch das Problem, dass in jedem Fall ein Anhang verlangt wird, ansonsten kommt die Meldung "nicht erlaubt".
Ich sehe den Fehler nicht, kann mich hier kurz mal jemand unterstützen?

PHP:
<?php
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);
ini_set('display_errors', 0);
include 'dbconnect.php';




if(isset($_FILES) && (bool) $_FILES) {

   $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png");

   $files = array();
   foreach($_FILES as $name=>$file) {
       $file_name = $file['name'];
       $temp_name = $file['tmp_name'];
       $file_type = $file['type'];
       $path_parts = pathinfo($file_name);
       $ext = $path_parts['extension'];
       if(!in_array($ext,$allowedExtensions)) {
           //die("File $file_name has the extensions $ext which is not allowed");
           die('<div class="alert alert-danger" role="alert"><strong>ACHTUNG! </strong>' . $file_name . ' ist nicht erlaubt!</div>');
       }
       array_push($files,$file);
   }



   // email fields: to, from, subject, and so on
   $to = "";
   $from = $_REQUEST['mail'];
   $subject ="Online-Abmeldung Übung - Grund: " . $_REQUEST['grund'];
   $message =
       "Datum der Übung: " . $_REQUEST['datum'] . "\n" .
         "Absender: " . $_REQUEST['anrede'] . " " . $_REQUEST['nachname'] . " " . $_REQUEST['vorname'] . "\n" .
         "Grad in der Feuerwehr LePiMe: " . $_REQUEST['grad'] . "\n" .
       "Bemerkung: "  . $_REQUEST['bemerkung'];
   $headers = "From: $from";

   // Alle Felder für die Datenbankspeicherung
   $datum=$_REQUEST['datum'];
   $anrede=$_REQUEST['anrede'];
   $nachname=$_REQUEST['nachname'];
   $vorname=$_REQUEST['vorname'];
   $grad=$_REQUEST['grad'];
   $mail=$_REQUEST['mail'];
   $grund=$_REQUEST['grund'];
   $bemerkung=$_REQUEST['bemerkung'];


   //insert
   $query=mysqli_query($db_connect, "INSERT INTO TABLE(
                                   datum, anrede, name, vorname, grad, email, grund, bemerkung)
                                    VALUES('$datum','$anrede','$nachname','$vorname','$grad','$mail','$grund','$bemerkung')") or die(mysqli_error($db_connect));
   mysqli_close($db_connect);


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

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

   // multipart boundary
   $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";
   $message .= "--{$mime_boundary}\n";

   // preparing attachments
   for($x=0;$x<count($files);$x++){
       $file = fopen($files[$x]['tmp_name'],"rb");
       $data = fread($file,filesize($files[$x]['tmp_name']));
       fclose($file);
       $data = chunk_split(base64_encode($data));
       $name = $files[$x]['name'];
       $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
       "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
       "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
       $message .= "--{$mime_boundary}\n";
   }
   // send
   $ok = mail($to, $subject, $message, $headers);
   if ($ok) {
       echo '<div class="alert alert-success" role="alert"><strong>Vielen Dank für die Mitteilung! </strong></div>';
   } else {
       echo '<div class="alert alert-danger" role="alert"><strong>ACHTUNG! </strong> Mail nicht versendet!</div>';
   }
}

//

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
   href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css"
   type="text/css" media="all">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript"
   src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"
   src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
<script>
       $(function(){
               $("#to").datepicker({ dateFormat: 'dd.mm.yy' });
               $("#from").datepicker({ dateFormat: 'dd.mm.yy' }).bind("change",function(){
                   var minValue = $(this).val();
                   minValue = $.datepicker.parseDate("dd.mm.yy", minValue);
                   minValue.setDate(minValue.getDate()+1);
                   $("#to").datepicker( "option", "minDate", minValue );
               })
           });
   </script>
</head>

<body>
<div class="container">
<form method="post" action="index.php" enctype="multipart/form-data">
<div class="form-group row"><label for="datum"
   class="col-sm-2 form-control-label">Datum:</label>
<div class="col-sm-10"><input type="text" class="form-control"
   name="datum" autocomplete="off" id="from" required="true"></div>
</div>

<p style="display:none;"><input type="text" name="url" /></p>


<div class="form-group row"><label for="anrede"
   class="col-sm-2 form-control-label">Anrede:</label>
<div class="col-sm-10"><select name="anrede"
   class="form-control form-control-sm" required="true">
   <option value="leer"></option>
   <option value="Herr">Herr</option>
   <option value="Frau">Frau</option>
</select></div>
</div>

<div class="form-group row"><label for="nachname"
   class="col-sm-2 form-control-label">Name:</label>
<div class="col-sm-10"><input type="text" class="form-control"
   name="nachname" autocomplete="off" value="" required="true"></div>
</div>

<div class="form-group row"><label for="vorname"
   class="col-sm-2 form-control-label">Vorname:</label>
<div class="col-sm-10"><input type="text" class="form-control"
   name="vorname" autocomplete="off" value="" required="true"></div>
</div>

<div class="form-group row"><label for="grad"
   class="col-sm-2 form-control-label">Grad:</label>
<div class="col-sm-10"><select name="grad"
   class="form-control form-control-sm" required="true">
   <option value="leer"></option>
   <option value="Rekrut">Rekrut</option>
   <option value="Soldat">Soldat</option>
   <option value="Korporal">Korporal</option>
   <option value="Wachtmeister">Wachtmeister</option>
   <option value="Leutnant">Leutnant</option>
   <option value="Oberleutnant">Oberleutnant</option>
   <option value="Hauptmann">Hauptmann</option>
   <option value="Major">Major</option>
</select></div>
</div>

<div class="form-group row"><label for="mail"
   class="col-sm-2 form-control-label">E-Mail:</label>
<div class="col-sm-10"><input type="text" class="form-control"
   name="mail" autocomplete="off" value="" required="true"></div>
</div>

<div class="form-group row"><label for="grund"
   class="col-sm-2 form-control-label">Grund:</label>
<div class="col-sm-10"><select name="grund"
   class="form-control form-control-sm" required="true">
   <option value="leer"></option>
   <option value="Krankheit">Krankheit</option>
   <option value="Unfall">Unfall</option>
   <option value="Todesfall Familie">Todesfall Familie</option>
   <option value="Schwangerschaft">Schwangerschaft</option>
   <option value="Milit&auml;r">Milit&auml;r</option>
   <option value="Beruflich">Berufliche Abwesenheit</option>
   <option value="Ferien">Ferien</option>
   <option value="Arbeitgeber">Arbeitgeber</option>
   <option value="Aus&uuml;bung &ouml;ffentliches Amt">Aus&uuml;bung
   &ouml;ffentliches Amt</option>
   <option value="Andere">Andere</option>
</select></div>
</div>

<div class="form-group row"><label for="bemerkung"
   class="col-sm-2 form-control-label">Bemerkung:</label>
<div class="col-sm-10"><textarea name="bemerkung" class="form-control"
   rows="3"></textarea></div>
</div>

<div class="form-group row"><label for="anhang"
   class="col-sm-2 form-control-label">Anhang:</label>
<div class="col-sm-10"><label class="btn btn-primary"
   for="my-file-selector"> <input type="file" name="attach1" /> </label></div>
</div>

<div class="form-group row"><label for="submit"
   class="col-sm-2 form-control-label"></label>
<div class="col-sm-10"><input type="submit" value="Senden" /></div>
</div>
</form>
</div>
</body>
</html>
 
#2
Hi

intuitiv ist dein Code schon richtig. Das Problem ist, dass PHP auch für Nicht-Uploads Arrayeinträge anlegt und dort einen Error-Wert setzt. Füg folgende Zeile gleich am Anfang der Schleife ein, um solche Einträge zu ignorieren:
PHP:
foreach($_FILES as $name=>$file) {

    if(isset($file['error']) && $file['error'] != 0) continue; //neu

    $file_name = $file['name'];