I have a file csv with code:
// put data to file
$file = 'file.csv';
$fp = fopen($file, 'w');
foreach ($excel as $fields) {
fputcsv($fp, $fields);
}
// download file when click button:
if ($this->request->getPost('cvs')) { {
if (file_exists($file)) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=' . ($file));
header('Pragma: no-cache');
readfile('/var/www/admin/backend/public/' . $file);
// OR readfile($file);
}
}
Data in file.csv (file.csv in publuc folder):
[Time,A1,A7,A30
03/24/2015,42531,130912,315805
03/25/2015,41124,132746,319098
03/26/2015,41050,134858,320934
03/27/2015,38687,134679,321747]
But when i click button to download file, data in file dowloaded is all html of page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">...etc..
How to fix it? Thanks very much!