загруженный файл не прикреплен к почте

моя HTML-форма для загрузки файла

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="file" id="file" name="files[]" multiple="multiple"  />
<input type="submit" id="submit" name="submit" value="Submit"/>
</form>

И PHP-код для отправки почты:

$to = "my email id";
$subject ="Registration";
$message ="<div style='border: 5px solid #B8B8B8;'><table border='0'cellspacing='0' cellpadding='10' >
<tr><td colspan='4'>Hi ,</td></tr>
<tr><td colspan='4'>A new report</td></tr>
<tr><td colspan='4'>Details are:</td></tr>
<tr> <td><table  width='100%'>
<tr><td><table border='1'cellspacing='0' cellpadding='0' width='98%'>
<tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;' >Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ".$sname."</td><td colspan='3'  style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Email&nbsp;&nbsp;&nbsp;: ".$email."</td></tr>
<tr> <td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Age&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ".$age."</td><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Date&nbsp;&nbsp;&nbsp;&nbsp;: ".$dt."</td></tr>
<tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Gender&nbsp;&nbsp;: ".$gender."</td><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Mobile&nbsp;: ".$mob."</td></tr>
<tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Center&nbsp;&nbsp;&nbsp;: ".$lname."</td><td colspan='3'  style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>History&nbsp;: ".$history."</td></tr>
<tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Address : ".$adrs."</td></tr></table></td></tr></table></td></tr>
<tr><td colspan='3'>&nbsp;</td></tr>
<tr><td><table width='98%'>
<tr><td colspan='3' align='center'><b>(DEEG) REPORT</b></td></tr>
<tr><td colspan='3'>&nbsp;</td></tr>
<tr><td colspan='3'><b>Clinical Interpretation :</b></td></tr>
<tr><td colspan='3' align='justify'> ".$clinic."</td></tr><br/>
<tr><td colspan='3'><b>Report :</b></td></tr>
<tr><td colspan='3' align='justify'> ".$report."</td></tr><br/>
<tr><td colspan='3'><b>EKG :</b></td></tr>
<tr><td colspan='3' align='justify'> ".$sumry."</td></tr><br/>
<tr><td colspan='3'><b>Activation Procedures :</b></td></tr>
<tr><td colspan='3' align='justify'> ".$act."</td></tr><br/>
<tr><td colspan='3'><b>Sleep  :</b></td></tr>
<tr><td colspan='3' align='justify'> ".$sleep."</td></tr></table></td></tr>
</table></div>";
$headers = "From: $lemail";
$semi_rand = md5(time());
$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";

foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){

// open the file for a binary read
$file = fopen($tmp_name,'rb');

// read the file content into a variable
$data = fread($file,filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}

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

$message .= "--{$mime_boundary}--\n";

$ok = @mail($to, $subject, $message, $headers);

Приведенный выше код отправляет почту без прикрепления файлов, а также показывает предупреждение

Warning: file_exists() expects parameter 1 to be string, array given in ..my file

Пожалуйста, помогите мне найти проблему. Я использовал тот же код в другом проекте, и он работал хорошо, но единственное отличие состоит в том, что в этом проекте я использовал 4 отдельных входа для загрузки файлов, но здесь я не могу использовать так.

редактировать

После того, как я использовал swiftmailer, я получил эти ошибки

Warning: basename() expects parameter 1 to be string, array given in C:\wamp\www\php\Teleneurology\swiftmailer-master\lib\classes\Swift\Mime\Attachment.php on line 139

Warning: strrpos() expects parameter 1 to be string, array given in C:\wamp\www\php\Teleneurology\swiftmailer-master\lib\classes\Swift\Mime\Attachment.php on line 143

Выход из var_dump($message) является:

string(2945) "This is a multi-part message in MIME format. --==Multipart_Boundary_x65d975657f4969833a0b685447467ab3x Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit

Hi ,
A new report
Details are:
Name     :  Email   :
Age        :    Date    : 21/10/2014
Gender  :   Mobile :
Center   :  History :
Address :
DIGITAL ELECTROENCEPHALOGRAPHY (DEEG) REPORT

Clinical Interpretation :
Select
Report :
Select
EKG :
Select
Activation Procedures :
Select
Sleep Record :
Select
--==Multipart_Boundary_x65d975657f4969833a0b685447467ab3x-- "

0

Решение

$_FILES как многоуровневый массив. Если бы вы повторили print_r($_FILES);, вы бы увидели проблему.

Попробуйте изменить:

foreach($_FILES as $userfile){
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

чтобы:

foreach ($_FILES['files']['name'] as $index => $name) {
$tmp_name = $_FILES['files']['tmp_name'][$index];
$type = $_FILES['files']['type'][$index];
$size = $_FILES['files']['size'][$index];
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector