-1

我试图从数据库中获取数据并将其显示在一个 CSV 文件中,该文件既可下载又可通过电子邮件发送给某人。我设法让可下载的文件正常工作,它显示了所有正确的数据。它还会向必要的人发送一个 CSV 文件,但该 CSV 文件是空的,并且其中没有显示任何数据。

这是我的代码:

    $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 文件是空的吗?

我有点绝望和压力很大:(请有人帮助我。

4

1 回答 1

0

它会帮助您查看错误日志或至少设置

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

因为那会告诉您 base64_encode 需要一个字符串,但 $output 是一种资源。

尝试设置

ob_start(); 

到开始和

$output = ob_get_flush();

在您的 fclose 和 $encoded 行之间。

还没有尝试过邮件,但这至少应该对您有所帮助:)

我尝试了这段代码,一切正常:

<?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));
于 2013-05-15T08:58:25.037 回答