php - Instead of saving CSV file how to Download the file -
i exporting data database using csv extension , want download results csv file. how can that?
this code:
public function actionexportexcel() { $con = mysql_connect("localhost", "root", "") or die(); mysql_select_db("fiducial", $con); $filename = 'uploads/'.strtotime("now").".csv"; $query = mysql_query("select * ( select employeecode,name,dob,age,sex,employee_relation,company_id employeedetails union select employeecode,father_name,father_dob,father_age,father_gender,father_relation,company_id employeedetails father_name not null , company_id not null union select employeecode,mother_name,mother_dob,mother_age,mother_gender,mother_relation,company_id employeedetails mother_name not null , mother_dob not null )t t.company_id = '56' order t.employeecode ") or die(mysql_error()); $num_rows = mysql_num_rows($query); if($num_rows >= 1) { $rows = mysql_fetch_assoc($query); $seperator = ""; $comma = ""; foreach ($rows $name => $value) { $seperator .= $comma . '' .str_replace('', '""', $name); $comma = ","; } $seperator .= "\n"; $fp = fopen($filename, "w"); fputs($fp, $seperator); mysql_data_seek($query, 0); while($rows = mysql_fetch_assoc($query)) { $seperator = ""; $comma = ""; foreach ($rows $name => $value) { $seperator .= $comma . '' .str_replace('', '""', $value); $comma = ","; } $seperator .= "\n"; fputs($fp, $seperator); } echo "data exported"; fclose($fp); } else { echo "no data available"; } } how can download csv file?
you need set http headers correctly csv file download , instead of writing query results csv file on local server, need write php output buffer (php://output):
full working example:
public function actionexportexcel() { $con = mysql_connect("localhost", "root", "") or die(); mysql_select_db("fiducial", $con); $filename = 'uploads/'.strtotime("now").".csv"; $query = mysql_query("select * ( select employeecode,name,dob,age,sex,employee_relation,company_id employeedetails union select employeecode,father_name,father_dob,father_age,father_gender,father_relation,company_id employeedetails father_name not null , company_id not null union select employeecode,mother_name,mother_dob,mother_age,mother_gender,mother_relation,company_id employeedetails mother_name not null , mother_dob not null )t t.company_id = '56' order t.employeecode ") or die(mysql_error()); $num_rows = mysql_num_rows($query); if($num_rows >= 1) { header('content-description: download name '); header('content-type: application/csv'); header('content-disposition: attachment; filename=yourfilename.csv'); $rows = mysql_fetch_assoc($query); $seperator = ""; $comma = ""; foreach ($rows $name => $value) { $seperator .= $comma . '' .str_replace('', '""', $name); $comma = ","; } $seperator .= "\n"; $fp = fopen('php://output', 'w'); fputs($fp, $seperator); mysql_data_seek($query, 0); while($rows = mysql_fetch_assoc($query)) { $seperator = ""; $comma = ""; foreach ($rows $name => $value) { $seperator .= $comma . '' .str_replace('', '""', $value); $comma = ","; } $seperator .= "\n"; fputs($fp, $seperator); } fclose($fp); } else { echo "no data available"; } }
Comments
Post a Comment