i have script that should export .csv file from database. Problem is that it will save export to specific ($f) file but wont download correct file (script download empty file)
<?php
//load the database configuration file
include '../secure/db_connect.php';
//get records from database
$sql_list = "SELECT * FROM `hakom` ORDER BY id DESC";
$sql_list_result = $mysqli->query($sql_list);
if($sql_list_result->num_rows > 0){
$delimiter = ",";
$filename = "members_" . date('Y-m-d') . ".csv";
//create a file pointer
$f = fopen('hakom_export.csv', 'w');
//set column headers
$fields = array('ID', 'MSISDN_');
fputcsv($f, $fields, $delimiter);
//output each row of the data, format line as csv and write to file pointer
while($row = $sql_list_result->fetch_assoc()){
$lineData = array($row['ID'], $row['MSISDN_']);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
}
exit;
?>
i follow tutorial on this page