0

I am having a problem that I just can't solve and I'm going madness with this! Basically I have to export a csv file. The site is obviously running on a server, and the idea is that onclick the file is generated and automatically downloaded. Everything is just fine, however, the file is being download to the server directory instead of the local user machine. I just can't make it.

What is wrong? Thank you very much!

if(!isset($crm_contas)) 
    $crm_contas = new crm_contas;

$resultGetValues = $crm_contas->getExportContas($filtros, $idsToSend);  

// filename for download
$filename = "ContasExportadas" . date('Ymd') . ".csv";

$output = fopen($filename, 'w');

foreach($resultGetValues as $row) {

    fputcsv($output, $row);
}

fclose($output);
$filename = realpath($filename);

$fp = @fopen($filename, 'rb');

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
    {
    header('Content-Type: "application/octet-stream"');
    header('Content-Disposition: attachment; filename='.$filename);
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Content-Transfer-Encoding: binary");
    header('Pragma: public');
    header("Content-Length: ".filesize($filename));
} else {
    header('Content-Type: "application/octet-stream"');
    header('Content-Disposition: attachment; filename='.$filename);
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".filesize($filename));
}

fpassthru($fp);
fclose($fp);`

--> This is the action that calls the code above.

echo "<a href='#' onclick='changeFormAction(\"form_list\",\"".$CONF['HOME']."/contas/index.php?view=export\");document.form_list.submit();changeFormAction(\"form_list\",pesquisa);'><img src='".$CONF['HOME']."/images/structure/excel.png' alt='Exportar' title='Exportar'></a>";

Meanwhile, I noticed that error_log is writting down this lines when I try do download. [21-Apr-2016 16:23:44 UTC] PHP Warning: Module 'imap' already loaded in Unknown on line 0

  • This looks like a duplicate question. Take a look at http://stackoverflow.com/questions/11903436/write-a-text-file-and-force-to-download-with-php which has links to other similar posts and you should find your answer. – Fergal Andrews Apr 21 '16 at 15:34
  • Hi. I've already tried that but what happens is that with readfile he justs displays the info on the website instead of downloading. I'm telling you, I'm going madness. – Gustavo Moreira Apr 21 '16 at 15:40
  • It can be frustrating sometimes Custavo. Try some of the other links from that question where people provide alternative solutions. – Fergal Andrews Apr 21 '16 at 15:41
  • My last 24 hours where spent trying to solve this. Almost all the answers have the same solution, using the readfile and the right headers. But to me, apparently, that is not enough :p – Gustavo Moreira Apr 21 '16 at 15:49
  • The assembled knowledge from a bunch of searches seems to be that you should use `header('Content-Type: application/csv');` and lose the `header("Content-Transfer-Encoding: binary");` See [this post](http://stackoverflow.com/a/16251849/2310830) – RiggsFolly Apr 21 '16 at 15:58
  • Thank you for the reply RiggsFolly. I still couldn't do it. It continues displaying on the web application instead of download it. I should be missing something really small! – Gustavo Moreira Apr 21 '16 at 16:17

0 Answers0