上传不起作用:邮件形式上传图像到服务器

问题描述 投票:0回答:1

我想为它发送1)邮件的邮件和2)当按下提交按钮也将文件上传到服务器。

我现在的问题是在2点邮件形式的作品了。我使用Ajax和PHP它。当我按下提交邮件发送,但没有被上传到我的目录。我没有得到任何错误..没有人有一个想法?

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

$currentDir = getcwd();
$uploadDirectory = "uploads/";

$errors = []; // Store all foreseen and unforseen errors here

$fileExtensions = ['jpeg', 'jpg', 'png']; // Get all the file extensions

$fileName = $_FILES['uploaded_file']['name'];
$fileSize = $_FILES['uploaded_file']['size'];
$fileTmpName = $_FILES['uploaded_file']['tmp_name'];
$fileType = $_FILES['uploaded_file']['type'];
$fileExtension = strtolower(end(explode('.', $fileName)));

$uploadPath = $currentDir . $uploadDirectory . basename($fileName);

if (isset($_POST['submit'])) {

    if (!in_array($fileExtension, $fileExtensions)) {
        $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
    }

    if ($fileSize > 2000000) {
        $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
    }

    if (empty($errors)) {
        $didUpload = move_uploaded_file($fileTmpName, $uploadPath);

        if ($didUpload) {
            echo "The file " . basename($fileName) . " has been uploaded";
        } else {
            echo "An error occurred somewhere. Try again or contact the admin";
        }
    } else {
        foreach ($errors as $error) {
            echo $error . "These are the errors" . "\n";
        }
    }
}

$from = '';
$sendTo = '';
$Bcc = '';

// subject of the email
$subject = 'Neue Nachricht von ';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('firma' => 'Firma', 'anrede' => 'Anrede', 'vorname' => 'Vorname', 'nachname' => 'Nachname', 'strasse' => 'Strasse', 'hausnummer' => 'Hausnummer', 'plz' => 'Plz',
    'ort' => 'Ort', 'liefer_strasse' => 'Lieferadresse Straße', 'liefer_hausnummer' => 'Lieferadresse Hausnummer', 'liefer_plz' => 'Lieferadresse PLZ', 'liefer_ort' => 'Lieferadresse Ort', 'lieferant_name' => 'Lieferant Name', 'lieferant_email' => 'Lieferant E-Mail',
    'lieferant_phone' => 'Lieferant Tel', 'lieferant_need' => 'Wie haben Sie von uns erfahren?', 'message' => 'Nachricht');

$okMessage = 'Ihre Anfrage wurde erfolgreich abgeschickt. Vielen Dank, wir werden uns zeitnah bei Ihnen melden.';


$errorMessage = 'Es gab einen Fehler beim abschicken des Formulars. Bitte probieren Sie es später nocheinmal oder Schreiben Sie uns direk';


// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try {

    if (count($_POST) == 0) throw new \Exception('Kontaktformular ist leer');

    $emailText = "Eine neue Nachricht vom Kontaktformular\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
        'Bcc:' . $Bcc
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
} // else just display the message
else {
    echo $responseArray['message'];
}

// function for lieferadress

$(document).ready(function() {
    $('#CheckLiefer2').change(function(){
        if($("#CheckLiefer2").is(':checked')){
            $('#lieferadresse').show();
            $('.lieferadresse_req').prop('required',true);
        }
        else
        {
            $('#lieferadresse').hide();
            $('.lieferadresse_req').prop('required',false);
        }
    });
})

// ajax function for form
$(function () {

    // init the validator
    // validator files are included in the download package
    // otherwise download from http://1000hz.github.io/bootstrap-validator

    $('#contactForm').validator();


    // when the form is submitted
    $('#contactForm').on('submit', function (e) {

        // if the validator does not prevent form submit
        if (!e.isDefaultPrevented()) {
            var url = "contact2.php";

            // POST values in the background the the script URL
            $.ajax({
                type: "POST",
                url: url,
                data: $(this).serialize(),
                success: function (data)
                 {
                    console.log(this);
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;

                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';

                    if (messageAlert && messageText) {
                        // inject the alert to .messages div in our form
                        $('#contactForm').find('.messages').html(alertBox);
                        // empty the form
                        $('#contactForm')[0].reset();
                    }
                }
            });
            return false;
        }
    })
});
<html>
<head>
    <title>Billiger Verschiffen | <?php echo $title;?></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link href='https://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
    <link href='css/custom.css' rel='stylesheet' type='text/css'>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>
    <script src="script.js"></script>
</head>
<body>


<form id="contactForm" class="contact-form" method="post" action="contact2.php" role="form" enctype="multipart/form-data">

                    <div class="controls">
                        <div class="row">
                            <div class="col-md-12">
                                <h1>Infos zum Auftraggeber</h1>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="firma">Firma *</label>
                                    <input id="firma" type="text" name="firma" class="form-control" placeholder="Firma *" required="required" data-error="Firma wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="anrede">Anrede *</label>
                                    <select id="anrede" name="anrede" class="form-control" required="required" data-error="Bitte Anrede auswählen.">
                                        <option value=""></option>
                                        <option value="Herr">Herr</option>
                                        <option value="Frau">Frau</option>
                                    </select>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-5">
                                <div class="form-group">
                                    <label for="vorname">Firstname *</label>
                                    <input id="vorname" type="text" name="vorname" class="form-control" placeholder="Bitte Vornamen eingeben *" required="required" data-error="Vorname wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-5">
                                <div class="form-group">
                                    <label for="nachname">Lastname *</label>
                                    <input id="nachname" type="text" name="nachname" class="form-control" placeholder="Bitte Nachname eingeben *" required="required" data-error="Nachname wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-10">
                                <div class="form-group">
                                    <label for="straße">Straße *</label>
                                    <input id="straße" type="text" name="straße" class="form-control" placeholder="Bitte Straße eingeben *" required="required" data-error="Straße wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="hausnummer">Hausnummer*</label>
                                    <input id="hausnummer" type="text" name="hausnummer" class="form-control" placeholder="Bitte Hausnummer eingeben *" required="required" data-error="Hausnummer wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="plz">PLZ *</label>
                                    <input id="plz" type="text" name="plz" class="form-control" placeholder="Bitte PLZ eingeben *" required="required" data-error="PLZ wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="ort">ORT *</label>
                                    <input id="ort" type="text" name="ort" class="form-control" placeholder="Bitte Ort eingeben *" required="required" data-error="Hausnummer wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>

                        <div class="row" style="padding-top:12px;">
                            <div class="col-md-12">
                                <div class="form-check">
                                    <input type="checkbox" class="form-check-input" id="CheckLiefer">
                                    <label class="form-check-label" for="CheckLiefer">Diese Lieferadresse verwenden</label>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-check">
                                    <input type="checkbox" class="form-check-input" id="CheckLiefer2">
                                    <label class="form-check-label" for="CheckLiefer2">Eine andere Lieferadresse verwenden</label>
                                </div>
                            </div>
                        </div>

                        <div class="row" id="lieferadresse" style="display: none; padding-top:30px;">
                            <div class="col-md-12">
                                <h1>Lieferadresse</h1>
                            </div>
                            <div class="col-md-10">
                                <div class="form-group">
                                    <label for="liefer_straße">Straße *</label>
                                    <input id="liefer_straße" type="text" name="liefer_straße" class="form-control lieferadresse_req" placeholder="Bitte Straße eingeben *" data-error="Straße wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="liefer_hausnummer">Hausnummer*</label>
                                    <input id="liefer_hausnummer" type="text" name="liefer_hausnummer" class="form-control lieferadresse_req" placeholder="Bitte Hausnummer eingeben *"  data-error="Hausnummer wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="liefer_plz">PLZ *</label>
                                    <input id="liefer_plz" type="text" name="liefer_plz" class="form-control lieferadresse_req" placeholder="Bitte PLZ eingeben *"  data-error="PLZ wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="liefer_ort">ORT *</label>
                                    <input id="liefer_ort" type="text" name="liefer_ort" class="form-control lieferadresse_req" placeholder="Bitte Ort eingeben *" data-error="Hausnummer wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>

                        <div class="row" style="padding-top:30px;">
                            <div class="col-md-12">
                                <h1>Buchungsdokumente</h1>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="FileUpload1">Upload für aktuelles Angebot des bisherigen Spediteurs für die geplante Verschiffung</label>
                                    <input type="file" class="form-control-file" id="FileUpload1" required="required" name="uploaded_file">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="FileUpload2">Upload der Handelsrechnung für zu verschiffende Ware</label>
                                    <input type="file" class="form-control-file" id="FileUpload2" name="FileUpload2">
                                </div>
                            </div>
                        </div>

                        <div class="row" style="padding-top:30px;">
                            <div class="col-md-12">
                                <h1>Absenderinformationen</h1>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="lieferant_name">Name *</label>
                                    <input id="lieferant_name" type="text" name="lieferant_name" class="form-control" placeholder="Name eingeben *" required="required" data-error="Name wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="lieferant_email">Email *</label>
                                    <input id="lieferant_email" type="email" name="lieferant_email" class="form-control" placeholder="E-Mail Adresse eingeben *" required="required" data-error="E-Mail wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="lieferant_phone">Phone *</label>
                                    <input id="lieferant_phone" type="text" name="lieferant_phone" class="form-control" placeholder="Telefonnummer eingeben *" required="required" data-error="Telefonnummer wird benötigt.">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="lieferant_erfahren">Wir haben Sie von uns erfahren? *</label>
                                    <select id="lieferant_erfahren" name="lieferant_need" class="form-control" required="required" data-error="Bitte eine Antwort auswählen.">
                                        <option value=""></option>
                                        <option value="Sandro">Sandro von Eykels (Youtube-Kanal)</option>
                                        <option value="Google">Google Suche</option>
                                        <option value="Yahoo">Yahoo Suche</option>
                                        <option value="Werbeanzeige">Sonstige Werbeanzeige</option>
                                        <option value="Freunde">Von Bekannten/ Freunden</option>
                                        <option value="Sonstige">Sonstige</option>
                                    </select>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="message">Nachricht *</label>
                                    <textarea id="message" name="message" class="form-control" placeholder="Nachricht" rows="4" required="required" data-error="Bitte hinterlasse uns eine Nachricht."></textarea>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <input type="submit" id="submit" class="btn btn-success btn-send" value="Abschicken">
                            </div>
                            <div id="msgSubmit" class="h3 text-center hidden"></div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                    <div class="messages" style="padding-top:20px;"></div>
                </form>
                </body>
</html>
javascript php jquery html
1个回答
0
投票

检查上传文件夹的权限。您的Web服务器正在运行,因为用户可能无法访问文件写入到您的上传目录。使用FTP或SSH,你可以把它改成0755这应该让你写你上传到上传目录。

© www.soinside.com 2019 - 2024. All rights reserved.