PHP中用于邮件程序的空(0KB)附件

时间:2018-10-17 22:20:26

标签: php email post

我正在尝试使一个简单的PHP邮件程序正常工作,但无法弄清楚为什么在发送电子邮件时却收到空附件。希望有人可以看到我所缺少的。

我将其归结为一些基本知识,以帮助缩短本文,这是HTML-

<form action="http://www.example.co.uk/send_form_ybk.php" 
method="POST" enctype="multipart/form-data"  >
 <input type="file" name="csv_file[]" />
 <br/>

 <input type="file" name="csv_file[]" />
 <br/>

 <input type="file" name="csv_file[]" />
 <br/>

 <input type="submit" name="upload" value="Upload" />
 <br/>

</form> 

和PHP

<?php

if($_POST) {

    for($i=0; $i < count($_FILES['csv_file']['name']); $i++){

        $ftype[]       = $_FILES['csv_file']['type'][$i];
        $fname[]       = $_FILES['csv_file']['name'][$i];

    }


    // array with filenames to be sent as attachment
    $files = $fname;

    // email fields: to, from, subject, and so on
    $to = "ben@example.co.uk";
    $from = "ben@example.co.uk"; 
    $subject ="My subject"; 
    $message = "My message";
    $headers = "From: $from";

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

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

    // multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\r\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";

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

    // send

    $ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to!</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } 



}
?>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的数组引用已关闭。拥有

grep '3000.*ESTABLISHED'

您需要参考

<input type=file name=csv_file[]>

获取第一个文件

所以

$_FILES['csv_file'][0]

将为您更好地工作。...

好的,根据您的评论,您现在至少可以获取数据。

这是我的处理附件(或不附件...)的form2mail脚本对我有用的

for($i=0; $i < count($_FILES['csv_file']); $i++){
   $ftype[]=$_FILES['csv_file'][$i]['type'];
   $fname[]=$_FILES['csv_file'][$i]['name'];
}