php表单上传noname附件

时间:2015-03-11 01:33:20

标签: php forms upload email-attachments

我有这个代码,它的工作原理。我只有一个副作用,我想知道如何解决它..在我的形式我有三个文件选择器。用户可以,如果他想要,上传最多三个文件..如果用户上传三个文件我收到电子邮件加上附件没有问题,一切都很完美!但如果用户上传了一个或两个文件,我收到了邮件和附件,但我还收到了一个空的“noname”附件..有没有办法避免这个空的附件? (如果用户只上传了一个文件,我有两个空的附件,而如果他上传了两个文件我有一个空的附件......)

这里是代码:

  $to = "my.email@mail.com";
  $email = $_POST['email'];
  $subject = "Proposta di Vendita/Affitto";

  $nome = $_POST['v_nome'];
  $email = $_POST['v_email'];
  $tel = $_POST['v_tel'];
  $categoria = $_POST['categoria_2'];
  $tipologia = $_POST['tipologia_2'];
  $comune = $_POST['comune_2'];
  $camere = $_POST['v_camere'];  
  $bagni = $_POST['v_bagni']; 
  $stanze = $_POST['v_stanze']; 
  $balconi = $_POST['v_balconi']; 
  $terrazzi = $_POST['v_terrazzi']; 
  $box = $_POST['v_box']; 
  $cantina = $_POST['v_cantina']; 
  $trattamento_dati_personali = $_POST['checkbox_dati_personali']; 

  $query_1 = mysql_query("SELECT categoria.nome FROM categoria WHERE categoria.id = '$categoria'");

  while($rows=mysql_fetch_array($query_1)){
    $categoria = $rows['nome'];
  }

  $query_2 = mysql_query("SELECT tipologia.nome FROM tipologia WHERE tipologia.id = '$tipologia'");

  while($rows=mysql_fetch_array($query_2)){
    $tipologia = $rows['nome'];
  }

  $query_3 = mysql_query("SELECT comuni.nome FROM comuni WHERE comuni.id = '$comune'");

  while($rows=mysql_fetch_array($query_3)){
    $comune = $rows['nome'];
  }  

    $message .= "Ricevi questa email perché ti é stata inviata una richiesta dal Sig/Sig.ra:" . "<br /><br />";
    $message .= "<b>" . "Nome: " . "</b>" . $nome . "<br />";
    $message .= "<b>" . "Email: " . "</b>" . $email . "<br />";
    $message .= "<b>" . "Tel: " . "</b>" . $tel . "<br />";
    $message .= "<b>" . "Categoria: " . "</b>" . $categoria . "<br />";
    $message .= "<b>" . "Tipologia: " . "</b>" . $tipologia . "<br />";
    $message .= "<b>" . "Comune: " . "</b>" . $comune . "<br />";
    $message .= "<b>" . "Camere: " . "</b>" . $camere . "<br />";
    $message .= "<b>" . "Bagni: " . "</b>" . $bagni . "<br />";
    $message .= "<b>" . "Stanze: " . "</b>" . $stanze . "<br />";
    $message .= "<b>" . "Balconi: " . "</b>" . $balconi . "<br />";
    $message .= "<b>" . "Terrazzi: " . "</b>" . $terrazzi . "<br />";
    $message .= "<b>" . "Box: " . "</b>" . $box . "<br />";
    $message .= "<b>" . "Cantina: " . "</b>" . $cantina . "<br />";
    $message .= "<b>" . "Trattamento Dati Personali: " . "</b>" . $trattamento_dati_personali . "<br />";

    // Temporary paths of selected files  
    $file1 = $_FILES['file1']['tmp_name'];  
    $file2 = $_FILES['file2']['tmp_name'];  
    $file3 = $_FILES['file3']['tmp_name'];  

    // File names of selected files  
    $filename1 = $_FILES['file1']['name'];  
    $filename2 = $_FILES['file2']['name'];  
    $filename3 = $_FILES['file3']['name'];  

    // array of filenames to be as attachments  
    $files = array($file1, $file2, $file3);  
    $filenames = array($filename1, $filename2, $filename3);  

    // include the from email in the headers  
    $headers = "From: $email";  

    // boundary  
    $time = md5(time());  
    $boundary = "==Multipart_Boundary_x{$time}x";  

    // headers used for send attachment with email  
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";  

    // multipart boundary  
    $message = "--{$boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  
    $message .= "--{$boundary}\n";  

    // attach the attachments to the message  
    for($x = 0; $x < count($files); $x++){  
    $file = fopen($files[$x],"r");  
    $content = fread($file,filesize($files[$x]));  
    fclose($file);  
    $content = chunk_split(base64_encode($content));  
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";  
    $message .= "--{$boundary}\n";  
    }

    // sending mail  
    $sendmail = mail($to, $subject, $message, $headers);  

    // verify if mail is sent or not  
    if ($sendmail) {  
        echo "Richiesta inviata correttamente";  
        } else {  
            echo "Errore durante l'invio";  
    }
    header("Location: post_vendita.php");
    exit;

2 个答案:

答案 0 :(得分:0)

按照以下方式更改您的代码希望可行。

for($x = 0; $x < count($files); $x++){  
  if(file_exists($files[$x])) {
    $file = fopen($files[$x],"r");  

    $content = fread($file,filesize($files[$x]));  
    fclose($file);  
    $content = chunk_split(base64_encode($content));  
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . 
                " name=\"$files[$x]\"\n" . 
                "Content-Disposition: attachment;\n" . 
                " filename=\"$filenames[$x]\"\n" . 
                "Content-Transfer-Encoding: base64\n\n" . 
                $content . "\n\n";  
    $message .= "--{$boundary}\n";  
  }
}

答案 1 :(得分:0)

在这里添加一行:

 // attach the attachments to the message  
for($x = 0; $x < count($files); $x++){  

并改为:

  // attach the attachments to the message  
for($x = 0; $x < count($files); $x++){ 

if(!file_exists($files[$x]))continue;