2013-05-15 53 views
-1

我試圖從數據庫中獲取數據並將其顯示爲可下載並通過電子郵件發送給某人的CSV文件。我設法使可下載文件正常工作,並顯示所有正確的數據。它還會將CSV文件發送給必要的人員,但該CSV文件是空的,並且不會顯示任何數據。電子郵件中的PHP附件爲空

這裏是我的代碼:

$myroot = "../../"; 
    include($myroot . "inc/functions.php"); 
     // output headers so that the file is downloaded rather than displayed 
     header('Content-Type: text/csv; charset=utf-8'); 
     header('Content-Disposition: attachment; filename=surveys.csv'); 
     $output = fopen('php://output', 'w'); 
// 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, ',', '"'); 
} 
fclose($output); 

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

// create the email and send it off 

$subject = "File you requested from RRWH.com"; 
$from = "*****@*****.com"; 
$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("*****@*****.com", $subject, $message, $headers, "-f$from"); 

我花了一天時間,並在此半,但我不能看到問題。有人能告訴我爲什麼附加的CSV文件是空的嗎?

我得到那種絕望,並強調了:(請幫助我的人。

回答

0

它會幫助你在你的錯誤日誌考慮看看,或者至少設置

ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1); 

爲會告訴你,BASE64_ENCODE需要一個字符串,但$輸出是一個資源。

嘗試設置

ob_start(); 

的開頭和

$output = ob_get_flush(); 

您FCLOSE & $編碼行之間。

沒有tryed郵件還沒有,但這應該幫助你至少有點:)

我tryed此代碼,一切運作良好:

<?php 
ob_start(); 
header('Content-Type: text/csv; charset=utf-8'); 
header('Content-Disposition: attachment; filename=surveys.csv'); 

$output = fopen('php://output', 'w'); 
fputcsv($output, array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5')); 
$data = array(); 
$data[] = array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5'); 

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

$output = ob_get_flush(); 

$encoded = chunk_split(base64_encode($output)); 
+0

您好,我是菜鳥PHP開發人員,知道的很少。我會在哪裏放置ini_set行?我放置了ob_start()和echo($ output = ob_get_flush());在那裏你也指示了我,但是現在可下載的CSV文件是空的(之前沒有),並且電子郵件根本不再發送(之前發送過,但是有一個空的CSV文件) – user2381872

+0

ini_set()必須位於腳本的開頭此外它應該最後寫入$ output = ob_get_flush(); 我已經測試過上面的代碼,一切正常 – Frosti