jeudi 21 avril 2016

Download file from server to user machine php

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($yourfile, '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);`




Aucun commentaire:

Enregistrer un commentaire