douxian1892 2015-03-11 01:33
浏览 40
已采纳

php表单上传noname附件

I have this code and it works. I have only one side effect and I'd like to know how to solve it.. In my form I have three file selectors. The user can, if he want, upload up to three files.. If the user upload three files I receive the email plus the attachments without problems, everything is perfect! but if the user upload one or only two files I receive the mail plus the attachment but I have receive also an empty "noname" attachment.. Is there a way to avoid this empty attachment? (if the user uploads only one file I have two empty attachments while if he uploads two files I have one empty attachmetns..)

Here the code:

  $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 .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$boundary}\"";  

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

" . $message . "

";  
    $message .= "--{$boundary}
";  

    // 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\"};
" . " name=\"$files[$x]\"
" . "Content-Disposition: attachment;
" . " filename=\"$filenames[$x]\"
" . "Content-Transfer-Encoding: base64

" . $content . "

";  
    $message .= "--{$boundary}
";  
    }

    // 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条回答 默认 最新

  • dongyan5815 2015-03-11 01:42
    关注

    Change your code as follow hope will works.

    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\"};
    " . 
                    " name=\"$files[$x]\"
    " . 
                    "Content-Disposition: attachment;
    " . 
                    " filename=\"$filenames[$x]\"
    " . 
                    "Content-Transfer-Encoding: base64
    
    " . 
                    $content . "
    
    ";  
        $message .= "--{$boundary}
    ";  
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能