平。 我希望我页面上的用户能够上传文件并向我发送包含所有其他输入的电子邮件,包括上传的文件,但我无法使其正常工作。我现在很长一段时间都在苦苦挣扎,每次发送一个submision,我都会收到消息“Invalid file”。我收到包含所有数据的电子邮件但文件丢失了。 我做错了什么?
这是html表单:
<form action="senden-bewerbung.php" id="contactform" method="post" enctype="multipart/form-data">
<fieldset>
<div class="clearfix">
<div class="input">
<label>Name:</label>
<input type="text" name="name" required>
</div>
<div class="input">
<label>Email adresse:</label>
<input type="email" name="email" required>
</div>
</div>
<div class="clearfix">
<div class="input">
<label>Nachname:</label>
<input type="text" name="surename" required>
</div>
<div class="input">
<label>Telefonnummer:</label>
<input type="tel" name="tel" required>
</div>
<div class="clearfix">
<div class="input">
<label>PLZ:</label>
<input type="text" name="plz" required>
</div>
<div class="input">
<label>Stadt/Ort:</label>
<input type="text" name="stadt" required>
</div>
</div>
<div class="clearfix">
<div class="input">
<label>Land:</label>
<select name="land" id="izbira">
<option value="Deutschland">Deutschland</option>
<option value="Österreich">Österreich</option>
<option value="Slowenien">Slowenien</option>
<option value="Croatien">Croatien</option>
</select>
</div>
<div class="input">
<label>Geburtsdatum:</label>
<input type="date" name="geburtstag" required style="width:150px;">
</div>
</div>
<div class="clearfix">
<div class="input">
<label>Ich bewerbe mich für:</label>
<select name="arbeit" id="izbira">
<option value="Krankenschwester">Krankenschwester</option>
<option value="Krankenpfleger">Krankenpfleger</option>
<option value="Kinderkrankenschwester">Kinderkrankenschwester</option>
<option value="Kinderkrankenpfleger">Kinderkrankenpfleger</option>
<option value="Altenpflegerin">Altenpflegerin</option>
<option value="Altenpfleger">Altenpfleger</option>
<option value="Hauswirtschaftsangestellte">Hauswirtschaftsangestellte</option>
<option value="Hauswirtschaftsangestellter">Hauswirtschaftsangestellter</option>
<option value="Pflegedienstleiterin">Pflegedienstleiterin</option>
<option value="Pflegedienstleiter">Pflegedienstleiter</option>
</select>
</div>
<div class="input">
<label>Lebenslauf hochladen (.pdf oder .doc/.docx .jpeg .png):</label>
<input type="file" name="lebenslauf" id="lebenslauf" size="16"/>
</div>
</div>
<div class="clearfix">
<div class="input"><label>Weiterbildung</label>
<input id="box1" type="checkbox" name="weiterbildung"/>
<label for="box1" name="Ja">Intensivfachweiterbildung</label>
</div>
</div>
</div>
<div class="clearfix">
<button type="submit" class="button" value="Submit">Senden</button>
</div>
</fieldset>
</form>
这是php文件:
<?php
header('Content-Type: text/html; charset=utf-8');
if(isset($_POST['email'])) {
$email_to = "info@domain.com";
$email_subject = "Kontakt";
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['surename']) ||
!isset($_POST['email']) ||
!isset($_POST['tel']) ||
!isset($_POST['plz']) ||
!isset($_POST['geburtstag']) ||
!isset($_POST['stadt']) ||
!isset($_POST['land']) ||
!isset($_POST['arbeit']) ||
!isset($_POST['weiterbildung']))
$first_name = $_POST['name']; // required
$sure_name = $_POST['surename']; // required
$email_from = $_POST['email']; // required
$tel = $_POST['tel']; // required
$platz = $_POST['plz'];
$geburt = $_POST['geburtstag'];
$stadt = $_POST['stadt'];
$national = $_POST['land'];
$bewfur = $_POST['arbeit'];
$weiterb = $_POST['weiterbildung'];
$lebenslauf = $_FILES['lebenslauf']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'Die Email ist nicht gulting.<br />';
}
$email_message = "Daten uber den Bewerber:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$allowedExts = array("doc", "docx", "gif", "jpeg", "jpg", "pdf", "png");
$extension = end(explode(".", $_FILES["lebenslauf"]["name"]));
if(
(
($_FILES["file"]["type"] == "image/gif" ) ||
($_FILES["file"]["type"] == "image/jpeg" ) ||
($_FILES["file"]["type"] == "image/png" ) ||
($_FILES["file"]["type"] == "image/pjpeg")
)
&&
($_FILES["file"]["size"] < 400000)
&&
in_array($extension, $allowedExts)
)
if ($_FILES["uploads"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else {
echo "Upload: " . $_FILES["lebenslauf"]["name"] . "<br />";
echo "Type: " . $_FILES["lebenslauf"]["type"] . "<br />";
echo "Size: " . ($_FILES["lebenslauf"]["size"] / 4024) . " Kb<br />";
echo "Temp file: " . $_FILES["lebenslauf"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["lebenslauf"]["name"])) {
echo $_FILES["lebenslauf"]["name"] . " already exists. ";
}
else {
move_uploaded_file($_FILES["lebenslauf"]["tmp_name"],
"upload/" . $_FILES["lebenslauf"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["lebenslauf"]["name"];
}
}
else {
echo "Invalid file";
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Nachname: ".clean_string($sure_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telefonnummer: ".clean_string($tel)."\n";
$email_message .= "Platz: ".clean_string($platz)."\n";
$email_message .= "Geburtsdatumm: ".clean_string($geburt)."\n";
$email_message .= "Stadt: ".clean_string($stadt)."\n";
$email_message .= "Land: ".clean_string($national)."\n";
$email_message .= "Arbeit: ".clean_string($bewfur)."\n";
$email_message .= "Weiterbildung: ".clean_string($weiterb)."\n";
$email_message .= "Lebenslauf: ".clean_string($lebenslauf)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}
?>