1

我从我的数据库中提取数据并以表格格式显示在网页上。我在此页面上提供了一个链接,允许用户将此数据下载为 CSV 文件。到目前为止,这工作正常,当用户点击显示数据下方的链接时,它允许他们保存它。

它还发送带有 CSV 作为附件的电子邮件。但是目前附件中的 CSV 是空白的,我不知道为什么。

我想要的只是将数据库中的数据放入可下载的 CSV 中,以便也进入附加的 CSV,但我做不到。

有人可以帮助我吗?

这是我到目前为止的代码:

// Create CSV file
fputcsv($output, array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5'));

$mysql_connection = db_connect_enhanced('*******','******','******','******');
$query='SELECT * FROM ****.****';
$surveys = db_query_into_array_enhanced($mysql_connection, $query);
$count = count($surveys);
$data = array();
    for($i=0; $i<=$count; $i++){
    $data[] = array($surveys[$i]['FeedbackName'], $surveys[$i]['BranchName'], $surveys[$i]['FeedbackWebsite'], $surveys[$i]['FeedbackCompany'], $surveys[$i]['Question1'], $surveys[$i]['Question2'], $surveys[$i]['Question3'], $surveys[$i]['Question4'], $surveys[$i]['Question5']);  
}

foreach( $data as $row )  
{  
    fputcsv($output, $row, ',', '"');  
}  

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

// create the email and send it off

$subject = "File you requested from RRWH.com";
$from = "***************";
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/mixed;
   boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";

$message = '

This is a multi-part message in MIME format.

------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: text/plain;
       charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello

We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php
as a zip file.

Regards

------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: application/octet-stream;  name="';

$message .= "surveys.csv";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "surveys.csv";
$message .= '"

';
$message .= "$encoded";
$message .= '

------=_NextPart_001_0011_1234ABCD.4321FDAC--

';
mail("*************", $subject, $message, $headers, "-f$from");
fclose($output);  

感觉就像我很接近,但我看不到答案:(

4

2 回答 2

1

更改此行:

$message .= "$encoded";

有了这个:

$message .= $encoded;
于 2013-05-14T17:21:07.953 回答
0

这只是一个“建议”的答案:

这是我用来附加文件的方法,它对我有用,它可以帮助您“解决”您遇到的问题。

$file_path = "/path_to_your/$file_name";
$file_path_name = "$file_name"; // this file name will be used at receiver end 

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

$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$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: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
于 2013-05-14T17:32:00.237 回答