Ich habe diese AJAX
POST
Anfrage von AngularJS
kommen, ein JSON
Modell mit einigen Feldern und eine Datei:Parsing Formulardaten mit PHP
------WebKitFormBoundarywlEiXuTa9EkwFUWz
Content-Disposition: form-data; name="model"
{"fname":"and","lname":"and","email":"[email protected]","phone":"+3912345","position":"Marketing","startdate":"10/10/2017"}
------WebKitFormBoundarywlEiXuTa9EkwFUWz
Content-Disposition: form-data; name="file"; filename="InterviewPreparation.pdf"
Content-Type: application/pdf
------WebKitFormBoundarywlEiXuTa9EkwFUWz--
Ich bin ein PHP-Dummy-Entwickler. Wie kann ich die Anfrage mit PHP analysieren? Dieser Code funktioniert nicht:
<?php
// mail.php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
// validate the variables ========
if (empty($_POST['model.fname']))
$errors['First name'] = 'Required.';
if (empty($_POST['model.lname']))
$errors['Last Name'] = 'Required.';
if (empty($_POST['model.email']))
$errors['Email'] = 'Required.';
if (empty($_POST['model.phone']))
$errors['Phone'] = 'Required.';
if (empty($_POST['model.position']))
$errors['Position'] = 'Required.';
if (empty($_POST['model.startdate']))
$errors['Start Date'] = 'Required.';
// return a response ==============
$file = $_POST['file'];
if (! empty($file))
echo 'Uploaded file: ' + $file.name;
// response if there are errors
if (! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// if there are no errors, return a message
$data['success'] = true;
$data['message'] = 'Mail sent, good luck!';
}
// return all our data to an AJAX call
echo json_encode($data);
?>
Content-type: multipart/Formulardaten mit 2 Attributen: ‚Modell‘ ist ein JSON und ‚Datei‘ ist eine Datei – epavan