Reputation: 1
I have an e-mail form here in PHP. With text, ale other fields everything works fine. The problem is with uploading the file. Everything looks good for me. Even with the function for the file e-mails are perfectly sended. But there isn`t any attachement in the message. When I click "Wyślij wiadomość" (Send) I have a message about a problem with uploading the file. Does enyone know what goes wrong?
<?php
//--- początek formularza ---
if(empty($_POST['submit'])) {
?>
<form action="" method="post">
<table class="col-md-10 col-sm-12 col-xs-12">
<tr>
<td class="col-md-2">NAZWISKO:<br /><br/></td>
<td><input type="text" name="formName" /></td>
</tr>
<tr>
<td class="col-md-2">E-MAIL:<br /><br/></td>
<td><input type="text" name="formEmail"/></td>
</tr>
<tr>
<td class="col-md-2">ZAŁĄCZ KOSZTORYS:<br /><br/></td>
<td><input type="file" name="formFile" /></td>
</tr>
<tr>
<td class="formularzTresc col-md-2">TREŚĆ:<br /><br/></td>
<td><textarea name="formText"></textarea></td>
</tr>
<tr>
<td></td>
<td><p align="right"><input type="submit" name="submit" value="Wyślij wiadomość" /><p></td>
</tr>
</table>
</form>
<?php
} else {
//dane adresata
$email = '#';
//dane z formularza
$formName = $_POST['formName'];
$formEmail = $_POST['formEmail'];
$formText = $_POST['formText'];
$formFile = $_POST['formFile'];
if(!empty($formName) && !empty($formEmail) && !empty($formText)) {
//--- początek funkcji weryfikującej adres e-mail ---
function checkMail($checkmail) {
if(filter_var($checkmail, FILTER_VALIDATE_EMAIL)) {
if(checkdnsrr(array_pop(explode("@",$checkmail)),"MX")){
return true;
}else{
return false;
}
} else {
return false;
}
}
//--- koniec funkcji ---
if(checkMail($formEmail)) {
//dodatkowe informacje: ip i host użytkownika
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
//tworzymy szkielet wiadomości
//treść wiadomości
$mailText = "Treść wiadomości:\n$formText\nOd: $formName, $formEmail, $formFile ($ip, $host)";
//adres zwrotny
$mailHeader = "From: $formName <$formEmail>\r\n";
$mailHeader .= "Content-type: text/plain; charset=utf-8\r\n";
$target_path = "../przeslanePliki";
$target_path = $target_path . basename( $_FILES['formFile']['name']);
if(move_uploaded_file($_FILES['formFile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['formFile']['name']).
"Plik został wysłany";
} else{
echo "Wystąpił problem z przesłaniem pliku. Prosimy spróbować ponownie.";
}
//funkcja odpowiedzialna za wysłanie e-maila
@mail($email, 'Pytanie do eksperta', $mailText, $mailHeader) or die('Błąd: wiadomość nie została wysłana');
//komunikat o poprawnym wysłaniu wiadomości
echo 'Wiadomość została wysłana';
} else {
echo 'Adres e-mail jest niepoprawny';
}
} else {
//komunikat w przypadku nie powodzenia
echo 'Wypełnij wszystkie pola formularza';
}
//--- koniec formularza ---
}
?>
Upvotes: 0
Views: 125
Reputation: 22532
The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form
<form action="" method="post" enctype="multipart/form-data">
Upvotes: 1