0

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 ---
}
?>

2
  • 2
    What's the exact error message? Commented Apr 25, 2015 at 8:31
  • I dodnt know, I dont know how to write logs to the file or sth. I know that the else section here worked: if(move_uploaded_file($_FILES['formFile']['tmp_name'], $target_path)) { But thank to the man below I fixed this problem, but theres a new one now if works fine, theres an information that file has been sent but no attachement in my mailbox just a text message without it. Commented Apr 25, 2015 at 9:48

1 Answer 1

1

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">
Sign up to request clarification or add additional context in comments.

3 Comments

OK, now I have an information that file has been sent, but there`s no attachement in the message in my mailbox :/
you change $formFile = $_POST['formFile']; to $formFile = $_FILES['formFile'];
Ive changed it but still doesnt work. E-mail is sent but nothing attached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.