Antworten auf deine Fragen:
Neues Thema erstellen

Antworten zum Thema „Formular mit Uploadfunktion“

cmfa

Nicht mehr ganz neu hier

Hallo Freunde der Nacht,

meine Tochter soll in der Schule eine HP basteln. Soweit so gut. Darin enthalten soll ein Formular mit Uploadfunktion sein. Ich habe eines im Netz gefunden und es läuft auch so weit. Jedoch einen kleinen Fehler hat es.
Die hochgeladene Datei kommt per E-Mail Anhang + einer "unbekannte Anlage 0000.txt" die leer ist. Jedoch der Inhalt aus den Formularfeldern ist weg. Wenn kein File hochgeladen wird kommt der Inhalt der Formularfelder an. Ich habe schon eine ganze Weile gesucht jedoch ohne den Fehler zu finden. Vielleicht habt Ihr ja eine Idee wo der Fehler liegt.

VG CMFA

Aufruf:
PHP:
<?PHP session_start(); ?>

<?php
include_once("kontaktformular.php");
echo kontaktformular_fileupload(array("Mali@home.de"),"Onlinenachricht mit Datei Anhang","form2");
?>

Datei:
PHP:
if(!function_exists("kontaktformular_fileupload")){
function kontaktformular_fileupload($empf,$formname,$formid="cformup320",$pfad=""){
global $_POST,$_SESSION;

$allowed_endings  = array('jpg','pdf','zip','rar');    // Array mit allen erlaubten Dateiendungen:
$allowed_filesize = 5242880;                // Max. Dateigröße in Byte

$return = "";
$error = FALSE;

if(isset($formid) && !empty($formid)){
        $data['Anrede'] =  $_POST['anrede_'.$formid];
    $data['name'] = $_POST['name_'.$formid];
    $data['strnr'] = $_POST['strnr_'.$formid];
    $data['plzort'] = $_POST['plzort_'.$formid];
    $data['tel'] = $_POST['tel_'.$formid];
    $data['email'] = $_POST['email_'.$formid];
    $data['nachricht'] = $_POST['nachricht_'.$formid];
    $data['captcha'] = $_POST['captcha_'.$formid];

    $error = TRUE;

    // Für Versand aus Empfänger-Adresse auf jeden Fall einen Array machen:
    if(!is_array($empf))
        $empf = array($empf);

    if(isset($_POST['name_'.$formid]) && !empty($_POST['name_'.$formid]) &&
    isset($_POST['anrede_'.$formid]) && !empty($_POST['anrede_'.$formid]) &&
        isset($_POST['email_'.$formid]) && !empty($_POST['email_'.$formid]) && check_mail($_POST['email_'.$formid]) &&
    isset($_POST['nachricht_'.$formid]) && !empty($_POST['nachricht_'.$formid]) &&
    isset($_POST['captcha_'.$formid]) && !empty($_POST['captcha_'.$formid]) && md5($_POST['captcha_'.$formid]) == $_SESSION['antispam_'.$formid])
        {

        $error = FALSE;

    $absender = preg_replace( "/[^a-z0-9 !?:;,.\/_\-=+@#$&\*\(\)]/im", "",$_POST['email_'.$formid]);
    $absender = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$absender);
   
        $anrede = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$_POST['anrede_'.$formid]);
    $name = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$_POST['name_'.$formid]);
        $str = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$_POST['strnr_'.$formid]);
    $plz = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$_POST['plzort_'.$formid]);
    $tel = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$_POST['tel_'.$formid]);
    $txt = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "",$_POST['nachricht_'.$formid]);
        $mail_header = "From:".$name."<".$absender.">";

    $mail_content = "Anrede:     ".$anrede."
Name:       ".$name."
Strasse Hausnummer: ".$str."
PLZ Ort:    ".$plz."
Telefon:    ".$tel."
E-Mail:     ".$absender."
IP-Adresse: ".$_SERVER['REMOTE_ADDR']."

Nachricht:
".$txt."";

        // Datei-Anhang verarbeiten
        if(isset($_FILES['file_'.$formid]['name']) && $_FILES['file_'.$formid]['name'] != ""){
            $split = pathinfo($_FILES['file_'.$formid]['name']);
            $filename = $split['filename'];
            $fileType = $split['extension'];
                       
            // Erlaubte Endungen / Dateigröße überprüfen
            if(in_array($fileType,$allowed_endings) && $_FILES['file_'.$formid]['size'] <= $allowed_filesize){
                $boundary = strtoupper(md5(uniqid(time())));

                $mail_header .= "\nMIME-Version: 1.0"."";
                $mail_header .= "\nContent-Type: multipart/mixed;  boundary=\"".$boundary."\"";

                $mail_body  = "\nMIME-Version: 1.0"."";
                $mail_body .= "\nContent-Type: multipart/mixed;  boundary=\"".$boundary."\"";
                $mail_body .= "\n\nThis is a multi-part message in MIME format  --  Dies ist eine mehrteilige Nachricht im MIME-Format";

                // "Normalen" Text-Inhalt einfügen:
                $mail_body .= "\n--".$boundary."";
                $mail_body .= "\nContent-Type: text/plain";
                $mail_body .= "\nContent-Transfer-Encoding: 8bit\n";
                $mail_body .= $mail_content;
                            $mail_body .= "\n--".$boundary."";
                            echo $mail_content;
                $file_content = fread(fopen($_FILES['file_'.$formid]['tmp_name'],"r"),$_FILES['file_'.$formid]['size']);
                $file_content = chunk_split(base64_encode($file_content));

                $mail_body .= "\nContent-Type: ".mime_content_type($_FILES['file_'.$formid]['name'])."; name=\"".stripslashes($_FILES['file_'.$formid]['name'])."\"";
                $mail_body .= "\nContent-Transfer-Encoding: base64";
                $mail_body .= "\nContent-Disposition: attachment; filename=\"".stripslashes($_FILES['file_'.$formid]['name'])."\"";
                $mail_body .= "\n\n".$file_content."";
                $mail_body .= "\n--".$boundary."";
                echo $mail_content;
            }
            else
                $error = TRUE;
        }
        // Wenn kein Datei-Anhang ausgewählt wurde
        else{
            $mail_body = $mail_content;
            //echo $mail_content;
        }

        foreach($empf as $empf_mail){
            //echo $mail_header."<br /><hr><br />".$mail_body;
            mail($empf_mail,$formname,$mail_body,$mail_header);
        }

        $return .= "<br /><p style=\"border: 1px solid green; padding: 5px;\">
        <b>Ihre Bewerbung wurde erfolgreich verschickt und wird so schnell wie m&ouml;glich bearbeitet.</b></p>";
        }

    if(isset($_POST['submit_'.$formid]) && !empty($_POST['submit_'.$formid]) && $error){
        $return .= "<br /><p style=\"border: 1px solid red; padding: 5px;\">
        Sie haben nicht alle ben&ouml;tigen Felder (*) ausgef&uuml;llt oder den Spamschutzcode nicht richtig eingegeben.</p>";
        }
    }
else $formid = rand_String(12);
           /*value=\"".$data['anrede']."\"*/
$return .= "
<form enctype=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
<table cellpadding=\"0\" cellspacing=\"5\" class=\"formtab pluginwidth\">

    <tr>
        <td>Anrede *</td>
        <td>
                  <select name=\"anrede_".$formid."\" style=\"width: 150px; \">
                    <option> </option>
                    <option>Herr</option>
                    <option>Frau</option>
                  </select></td>
        </tr>

        <tr>
        <td>Vor- u. Nachname *</td>
        <td><input class=\"tx\" type=\"text\" name=\"name_".$formid."\" value=\"".$data['name']."\" style=\"width: 150px; \" /></td>
    </tr>
        <tr>
        <td>Strasse u. Hausnummer *</td>
        <td><input class=\"tx\" type=\"text\" name=\"strnr_".$formid."\" value=\"".$data['strnr']."\" style=\"width: 150px; \" /></td>
    </tr>
    <tr>
        <td>PLZ u. Ort</td>
        <td><input class=\"tx\" type=\"text\" name=\"plzort_".$formid."\" value=\"".$data['plzort']."\" style=\"width: 150px; \" /></td>
    </tr>

    <tr>
        <td>Telefon</td>
        <td><input class=\"tx\" type=\"text\" name=\"tel_".$formid."\" value=\"".$data['tel']."\" style=\"width: 150px; \" /></td>
    </tr>

    <tr>
        <td>E-Mail *</td>
        <td><input class=\"tx\" type=\"text\" name=\"email_".$formid."\" value=\"".$data['email']."\" style=\"width: 150px; \" /></td>
    </tr>

    <tr>
        <td>Datei max. 5 MB</td>
        <td><input type=\"file\" name=\"file_".$formid."\" /></td>
    </tr>

    <tr>
        <td valign=\"top\">Nachricht *</td>
        <td><textarea class=\"tx\" name=\"nachricht_".$formid."\">".$data['nachricht']."</textarea></td>
    </tr>

    <!--captcha-->
    <tr>
        <td>Sicherheitscode</td>
        <td><img src=\"".$pfad."secimg.php?formid=".$formid."\" alt=\"Sicherheitscode (Spamschutz)\" title=\"Sicherheitscode: Anti-Spam-System\" width=\"90\" height=\"30\" /></td>
    </tr>
    <tr>
      <td>Sicherheitscode eingeben *</td>
        <td>
          <input type=\"text\" class=\"tx\" name=\"captcha_".$formid."\" maxlength=\"6\" style=\"width: 150px; \" />
            </td>
    </tr>

    <input type=\"hidden\" name=\"formid\" value=\"".$formid."\" />
    <tr>
        <td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit_".$formid."\" value=\"Absenden\" /></td>
    </tr>
</table>
</form>";

return $return;

}
}

if(!function_exists("rand_String")){
function rand_String($laenge){
    mt_srand((double)microtime()*1000000);
    $zahl = mt_rand(1000, 9999);

    $passzahl = md5($zahl);
    $newpass = substr($passzahl,0,$laenge);

    return $newpass;
    }
}

if(!function_exists("check_mail")){
function check_mail($email){
    if(filter_var($email, FILTER_VALIDATE_EMAIL)) return TRUE;
    else return FALSE;
}
}

// Mime-Typen von Dateien bestimmen
/*$filename        Dateiname zu dem der Dateityp bestimmt werden soll
 
RETURN: Mime-Typ
  */
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {

    $mime_types = array(
        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',

        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    $ext = strtolower(array_pop(explode('.',$filename)));
    if (array_key_exists($ext, $mime_types)) {
        return $mime_types[$ext];
    }
    elseif (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME);
        $mimetype = finfo_file($finfo, $filename);
        finfo_close($finfo);
        return $mimetype;
    }
    else
        return 'application/octet-stream';

}
}
 

Duddle

Posting-Frequenz: 14µHz

Ich habe das Script lokal getestet und es funktioniert. Ich musste das Captcha ausbauen und für meine Tests die erlaubten Dateiendungen erweitern, aber grundsätzlich verschickt es die Mail mit dem Anhang.

Entweder versuchst du etwas zu verschicken, was du nicht darfst (siehe Zeile 5 im Script) oder dein Hoster erlaubt nicht das Senden sehr großer Mails o.ä. Grundsätzlich kannst du Mails ja verschicken (und kommst auch mit dem Captcha klar), also rate ich mal es könnte eine Beschränkung geben.


Duddle
 

cmfa

Nicht mehr ganz neu hier

Hi,

das das Script funktioniert stelle ich auch nicht in Frage. Wie ich schon geschrieben habe kommt der Anhang + einer zweiten Datei an. Nur die Formularfelder sind nicht in der Mail enthalten. Ich habe versucht das Problem zu lokalisieren und Tip auf die Zeile 79 ff.
Mein Hoster ist 1und1

VG CMFA
 

Duddle

Posting-Frequenz: 14µHz

Stimmt, bei mir kam die Nachricht auch nur teilweise an. Wenn du dir den Quelltext der Mail anschaust siehst du aber, dass die Informationen an sich drin sind, bspw.
Code:
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Anrede: Herr
Name: asd asdasd

Aber scheinbar müssen zwischen den Headern und dem Inhalt eine leere Zeile sein. Bei den Dateien wurde das eingehalten:
PHP:
      $mail_body .= "\n\n".$file_content."";
Beim Klartext aber nicht:
PHP:
      $mail_body .= "\nContent-Transfer-Encoding: 8bit\n";
       $mail_body .= $mail_content;
Wenn ich hinter dem 8bit noch ein "\n" einfüge, also eine leere Zeile, funktioniert es.


Duddle
 
Bilder bitte hier hochladen und danach über das Bild-Icon (Direktlink vorher kopieren) platzieren.
Antworten auf deine Fragen:
Neues Thema erstellen

Willkommen auf PSD-Tutorials.de

In unseren Foren vernetzt du dich mit anderen Personen, um dich rund um die Themen Fotografie, Grafik, Gestaltung, Bildbearbeitung und 3D auszutauschen. Außerdem schalten wir für dich regelmäßig kostenlose Inhalte frei. Liebe Grüße senden dir die PSD-Gründer Stefan und Matthias Petri aus Waren an der Müritz. Hier erfährst du mehr über uns.

Stefan und Matthias Petri von PSD-Tutorials.de

Nächster neuer Gratisinhalt

03
Stunden
:
:
25
Minuten
:
:
19
Sekunden

Flatrate für Tutorials, Assets, Vorlagen

Zurzeit aktive Besucher

Statistik des Forums

Themen
175.158
Beiträge
2.581.880
Mitglieder
67.226
Neuestes Mitglied
Alec168
Oben