从Mysql导出数据并输出到CSV允许另存为

时间:2015-11-04 14:47:42

标签: php mysql csv export

我想从mysql导出我的数据并将其导出为CSV(允许下载)或通过电子邮件发送出去。

问题:CSV文件为空。下载开始时,文件为空..

<?php
    require_once("../dbconnection/dbcon.php"); 

if(isset($_POST['download']))
{
    $filName = "customer.csv";
    $objWrite = fopen("customer.csv", "w");

    $query = "SELECT * FROM user";
    $run=mysqli_query($con,$query);

    while($objResult = mysqli_fetch_array($run))
    {
        fwrite($objWrite, "\"$objResult[username]\",\"$objResult[password]\",\"$objResult[name]\",");
        fwrite($objWrite, "\"$objResult[email]\",\"$objResult[role]\",\"$objResult[satus]\" \n");
    }
    fclose($objWrite);
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    // Output to browser with appropriate mime type, you choose ;)
    //header("Content-type: text/x-csv");
    //header("Content-type: text/csv");
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filName");

}

exit;
?>

我将CSV作为电子邮件发送的代码

<?php
$filName = "customer.csv";
$objWrite = fopen("customer.csv", "w");

require_once("../dbconnection/dbcon.php");

$query = "SELECT * FROM user";
$run=mysqli_query($con,$query);

while($objResult = mysqli_fetch_array($run))
{
    fwrite($objWrite, "\"$objResult[username]\",\"$objResult[password]\",\"$objResult[name]\",");
    fwrite($objWrite, "\"$objResult[email]\",\"$objResult[role]\",\"$objResult[satus]\" \n");
}
fclose($objWrite);

//*************** Send Email ***************//

$strTo = "my email";
$strSubject = "CSV Report";
$strMessage = "Download $filName for CSV Report";

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: RecruitMate | Murdoch University<recruitmate.mur@gmail.com>\nReply-To: recruitmate.mur@gmail.com\n";


$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 //
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

$strContent1 = chunk_split(base64_encode(file_get_contents("$filName")));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$filName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$filName."\"\n\n";
$strHeader .= $strContent1."\n\n";

$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //
if($flgSend)
{
echo "CSV Generated & Email Sending.";
}
else
{
echo "Email Can Not Send.";
}

?>

1 个答案:

答案 0 :(得分:0)

正如我所提到的 - 您的下载代码除了发送标题外什么都不做。但是你不发送文件内容。最简单的方法是使用readfile

// some headers here
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filName");
readfile($filName);
exit;