i want to export my data from mysql and export it to CSV (allow download) OR email it out.
Problem : the CSV file is empty. When the download started, the file is empty..
<?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;
?>
my code for sending the CSV as email works
<?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.";
}
?>
As I mentioned - your code for downloading does nothing except sending headers. But you don't send file contents. The easiest way is to use readfile
:
// some headers here
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filName");
readfile($filName);
exit;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.