用PHP发送CSV文件

我已经从我的数据库中提取数据,并以表格的forms显示在网页上。 我在这个页面上提供了一个链接,允许用户下载这个数据作为一个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); 

感觉就像我这么近,但我不能看到答案:(

改变这一行:

 $message .= "$encoded"; 

有了这个:

 $message .= $encoded; 

这只是一个“SUGGESTIVE”的答案:

这是我用来附加文件,它适用于我,它可能会帮助你“BASE”自己与你遇到的问题。

 $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";