1. Diese Seite verwendet Cookies. Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies. Weitere Informationen

Formular ohne Anhang versenden

Dieses Thema im Forum "PHP" wurde erstellt von Quu, 16. November 2016.

  1. Quu

    Quu Grünschnabel

    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?

    Code (PHP):
    1. <?php
    2. header('Content-Type: text/html; charset=utf-8');
    3. ini_set('display_errors', 0);
    4. include 'dbconnect.php';
    5.  
    6.  
    7.  
    8.  
    9. if(isset($_FILES) && (bool) $_FILES) {
    10.  
    11.    $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png");
    12.  
    13.    $files = array();
    14.    foreach($_FILES as $name=>$file) {
    15.        $file_name = $file['name'];
    16.        $temp_name = $file['tmp_name'];
    17.        $file_type = $file['type'];
    18.        $path_parts = pathinfo($file_name);
    19.        $ext = $path_parts['extension'];
    20.        if(!in_array($ext,$allowedExtensions)) {
    21.            //die("File $file_name has the extensions $ext which is not allowed");
    22.            die('<div class="alert alert-danger" role="alert"><strong>ACHTUNG! </strong>' . $file_name . ' ist nicht erlaubt!</div>');
    23.        }
    24.        array_push($files,$file);
    25.    }
    26.  
    27.  
    28.  
    29.    // email fields: to, from, subject, and so on
    30.    $to = "";
    31.    $from = $_REQUEST['mail'];
    32.    $subject ="Online-Abmeldung Übung - Grund: " . $_REQUEST['grund'];
    33.    $message =
    34.        "Datum der Übung: " . $_REQUEST['datum'] . "\n" .
    35.          "Absender: " . $_REQUEST['anrede'] . " " . $_REQUEST['nachname'] . " " . $_REQUEST['vorname'] . "\n" .
    36.          "Grad in der Feuerwehr LePiMe: " . $_REQUEST['grad'] . "\n" .
    37.        "Bemerkung: "  . $_REQUEST['bemerkung'];
    38.    $headers = "From: $from";
    39.  
    40.    // Alle Felder für die Datenbankspeicherung
    41.    $datum=$_REQUEST['datum'];
    42.    $anrede=$_REQUEST['anrede'];
    43.    $nachname=$_REQUEST['nachname'];
    44.    $vorname=$_REQUEST['vorname'];
    45.    $grad=$_REQUEST['grad'];
    46.    $mail=$_REQUEST['mail'];
    47.    $grund=$_REQUEST['grund'];
    48.    $bemerkung=$_REQUEST['bemerkung'];
    49.  
    50.  
    51.    //insert
    52.    $query=mysqli_query($db_connect, "INSERT INTO TABLE(
    53.                                   datum, anrede, name, vorname, grad, email, grund, bemerkung)
    54.                                    VALUES('$datum','$anrede','$nachname','$vorname','$grad','$mail','$grund','$bemerkung')") or die(mysqli_error($db_connect));
    55.    mysqli_close($db_connect);
    56.  
    57.  
    58.    // boundary
    59.    $semi_rand = md5(time());
    60.    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    61.  
    62.    // headers for attachment
    63.    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    64.  
    65.    // multipart boundary
    66.    $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";
    67.    $message .= "--{$mime_boundary}\n";
    68.  
    69.    // preparing attachments
    70.    for($x=0;$x<count($files);$x++){
    71.        $file = fopen($files[$x]['tmp_name'],"rb");
    72.        $data = fread($file,filesize($files[$x]['tmp_name']));
    73.        fclose($file);
    74.        $data = chunk_split(base64_encode($data));
    75.        $name = $files[$x]['name'];
    76.        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
    77.        "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
    78.        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    79.        $message .= "--{$mime_boundary}\n";
    80.    }
    81.    // send
    82.    $ok = mail($to, $subject, $message, $headers);
    83.    if ($ok) {
    84.        echo '<div class="alert alert-success" role="alert"><strong>Vielen Dank für die Mitteilung! </strong></div>';
    85.    } else {
    86.        echo '<div class="alert alert-danger" role="alert"><strong>ACHTUNG! </strong> Mail nicht versendet!</div>';
    87.    }
    88. }
    89.  
    90. //
    91.  
    92. ?>
    93.  
    94. <!DOCTYPE html>
    95. <html lang="en">
    96. <head>
    97. <meta charset="utf-8">
    98. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    99. <meta name="viewport" content="width=device-width, initial-scale=1">
    100. <link rel="stylesheet"
    101.    href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css"
    102.    type="text/css" media="all">
    103. <link href="css/bootstrap.min.css" rel="stylesheet">
    104. <script type="text/javascript"
    105.    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    106. <script type="text/javascript"
    107.    src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
    108. <script>
    109.        $(function(){
    110.                $("#to").datepicker({ dateFormat: 'dd.mm.yy' });
    111.                $("#from").datepicker({ dateFormat: 'dd.mm.yy' }).bind("change",function(){
    112.                    var minValue = $(this).val();
    113.                    minValue = $.datepicker.parseDate("dd.mm.yy", minValue);
    114.                    minValue.setDate(minValue.getDate()+1);
    115.                    $("#to").datepicker( "option", "minDate", minValue );
    116.                })
    117.            });
    118.    </script>
    119. </head>
    120.  
    121. <body>
    122. <div class="container">
    123. <form method="post" action="index.php" enctype="multipart/form-data">
    124. <div class="form-group row"><label for="datum"
    125.    class="col-sm-2 form-control-label">Datum:</label>
    126. <div class="col-sm-10"><input type="text" class="form-control"
    127.    name="datum" autocomplete="off" id="from" required="true"></div>
    128. </div>
    129.  
    130. <p style="display:none;"><input type="text" name="url" /></p>
    131.  
    132.  
    133. <div class="form-group row"><label for="anrede"
    134.    class="col-sm-2 form-control-label">Anrede:</label>
    135. <div class="col-sm-10"><select name="anrede"
    136.    class="form-control form-control-sm" required="true">
    137.    <option value="leer"></option>
    138.    <option value="Herr">Herr</option>
    139.    <option value="Frau">Frau</option>
    140. </select></div>
    141. </div>
    142.  
    143. <div class="form-group row"><label for="nachname"
    144.    class="col-sm-2 form-control-label">Name:</label>
    145. <div class="col-sm-10"><input type="text" class="form-control"
    146.    name="nachname" autocomplete="off" value="" required="true"></div>
    147. </div>
    148.  
    149. <div class="form-group row"><label for="vorname"
    150.    class="col-sm-2 form-control-label">Vorname:</label>
    151. <div class="col-sm-10"><input type="text" class="form-control"
    152.    name="vorname" autocomplete="off" value="" required="true"></div>
    153. </div>
    154.  
    155. <div class="form-group row"><label for="grad"
    156.    class="col-sm-2 form-control-label">Grad:</label>
    157. <div class="col-sm-10"><select name="grad"
    158.    class="form-control form-control-sm" required="true">
    159.    <option value="leer"></option>
    160.    <option value="Rekrut">Rekrut</option>
    161.    <option value="Soldat">Soldat</option>
    162.    <option value="Korporal">Korporal</option>
    163.    <option value="Wachtmeister">Wachtmeister</option>
    164.    <option value="Leutnant">Leutnant</option>
    165.    <option value="Oberleutnant">Oberleutnant</option>
    166.    <option value="Hauptmann">Hauptmann</option>
    167.    <option value="Major">Major</option>
    168. </select></div>
    169. </div>
    170.  
    171. <div class="form-group row"><label for="mail"
    172.    class="col-sm-2 form-control-label">E-Mail:</label>
    173. <div class="col-sm-10"><input type="text" class="form-control"
    174.    name="mail" autocomplete="off" value="" required="true"></div>
    175. </div>
    176.  
    177. <div class="form-group row"><label for="grund"
    178.    class="col-sm-2 form-control-label">Grund:</label>
    179. <div class="col-sm-10"><select name="grund"
    180.    class="form-control form-control-sm" required="true">
    181.    <option value="leer"></option>
    182.    <option value="Krankheit">Krankheit</option>
    183.    <option value="Unfall">Unfall</option>
    184.    <option value="Todesfall Familie">Todesfall Familie</option>
    185.    <option value="Schwangerschaft">Schwangerschaft</option>
    186.    <option value="Milit&auml;r">Milit&auml;r</option>
    187.    <option value="Beruflich">Berufliche Abwesenheit</option>
    188.    <option value="Ferien">Ferien</option>
    189.    <option value="Arbeitgeber">Arbeitgeber</option>
    190.    <option value="Aus&uuml;bung &ouml;ffentliches Amt">Aus&uuml;bung
    191.    &ouml;ffentliches Amt</option>
    192.    <option value="Andere">Andere</option>
    193. </select></div>
    194. </div>
    195.  
    196. <div class="form-group row"><label for="bemerkung"
    197.    class="col-sm-2 form-control-label">Bemerkung:</label>
    198. <div class="col-sm-10"><textarea name="bemerkung" class="form-control"
    199.    rows="3"></textarea></div>
    200. </div>
    201.  
    202. <div class="form-group row"><label for="anhang"
    203.    class="col-sm-2 form-control-label">Anhang:</label>
    204. <div class="col-sm-10"><label class="btn btn-primary"
    205.    for="my-file-selector"> <input type="file" name="attach1" /> </label></div>
    206. </div>
    207.  
    208. <div class="form-group row"><label for="submit"
    209.    class="col-sm-2 form-control-label"></label>
    210. <div class="col-sm-10"><input type="submit" value="Senden" /></div>
    211. </div>
    212. </form>
    213. </div>
    214. </body>
    215. </html>
     
  2. sheel

    sheel I love Asm Administrator

    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:
    1. foreach($_FILES as $name=>$file) {
    2.  
    3.     if(isset($file['error']) && $file['error'] != 0) continue; //neu
    4.  
    5.     $file_name = $file['name'];
     
  3. Quu

    Quu Grünschnabel

    ¨Genau das wars... herzlichen Dank!
     
Die Seite wird geladen...