J
J
JsNoobUser2021-06-16 17:05:24
PHP
JsNoobUser, 2021-06-16 17:05:24

Why is there only one file in the mail?

The form has enctype="multipart/form-data"

<input name="file" type="file" multiple="">

<?php
$to = '';

if ( isset( $_POST['email'] ) ) {
  $name  = substr( $_POST['name'], 0, 64 );
  $tel = substr( $_POST['phone'], 0, 64 );
  $email   = substr( $_POST['email'], 0, 64 );
  $message = substr( $_POST['comment'], 0, 250 );
  $formtype = substr( $_POST['formtype'], 0, 250 );

  
  if ( !empty( $_FILES['file']['tmp_name'] ) and $_FILES['file']['error'] == 0 ) {
    $filepath = $_FILES['file']['tmp_name'];
    $filename = $_FILES['file']['name'];
  } else {
    $filepath = '';
    $filename = '';
  }
 
  $body = "Имя:\r\n".$name."\r\n\r\n";
  $body .= "Контактный номер:\r\n".$tel."\r\n\r\n";
  $body .= "E-mail:\r\n".$email."\r\n\r\n";
  $body .= "Описание заказа:\r\n".$message."\r\n\r\n";
  $body .= "Откуда заявка:\r\n".$formtype;
 
  send_mail($to, $body, $email, $filepath, $filename);
}


function send_mail($to, $body, $email, $filepath, $filename)
{
  $subject = '';
  $boundary = "--".md5(uniqid(time())); 
  $headers = "From: Лендинг" . "\r\n"; 
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
  $multipart = "--".$boundary."\r\n";
  $multipart .= "Content-type: text/plain; charset=\"utf-8\"\r\n";
  $multipart .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";

  $body = $body."\r\n\r\n";
 
  $multipart .= $body;
 
  $file = '';
  if ( !empty( $filepath ) ) {
    $fp = fopen($filepath, "r");
    if ( $fp ) {
      $content = fread($fp, filesize($filepath));
      fclose($fp);
      $file .= "--".$boundary."\r\n";
      $file .= "Content-Type: application/octet-stream\r\n";
      $file .= "Content-Transfer-Encoding: base64\r\n";
      $file .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
      $file .= chunk_split(base64_encode($content))."\r\n";
    }
  }
  $multipart .= $file."--".$boundary."--\r\n";
  mail($to, $subject, $multipart, $headers);
}

ini_set('short_open_tag', 'On');
header('Refresh: 2; URL=index.html');
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2021-06-16
@JsNoobUser

Because only one file is added to the letter.

G
galaxy, 2021-06-16
@galaxy

In addition, I note that few people understand how it works <input name="file" type="file" multiple="">.
For some reason, they think that if you click on the download button several times, then several will be attached. In fact, with each next click and selection of a new file, the old one is overwritten.
Several files must be selected at once by holding down Ctrl.

S
Sergey, 2021-06-16
@sslion

kapets...
<input name="file[]" type="file" multiple>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question