附上两个文件PHP

时间:2013-07-31 12:53:56

标签: php email

我已经通过电子邮件发送了一个文件,但我被要求这样做可以接受两个。

到目前为止我的脚本看起来像。

// Obtain file upload vars
$fileatt      = $_FILES['file1']['tmp_name'];
$fileatt_type = $_FILES['file1']['type'];
$fileatt_name = $_FILES['file1']['name'];


if (is_uploaded_file($fileatt)) {


  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

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

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

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


  $data = chunk_split(base64_encode($data));

  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";

}

我希望能够在头部使用这些,并且能够发布。我已经尝试修改脚本来这样做,但我只得到一个文件。

// Obtain file upload vars
$fileatt2      = $_FILES['file2']['tmp_name'];
$fileatt_type2 = $_FILES['file1']['type'];
$fileatt_name2 = $_FILES['file1']['name'];

1 个答案:

答案 0 :(得分:0)

所以你现在在HTML中有2个输入字段

<input name="file1" type="file" />
<input name="file2" type="file" />

这是你的WOOPS

// Obtain file upload vars
$fileatt2      = $_FILES['file2']['tmp_name'];
$fileatt_type2 = $_FILES['file1']['type'];
$fileatt_name2 = $_FILES['file1']['name'];

将全部3改为使用file2

// Obtain file upload vars
$fileatt2      = $_FILES['file2']['tmp_name'];
$fileatt_type2 = $_FILES['file2']['type'];
$fileatt_name2 = $_FILES['file2']['name'];