Search code examples
phpdownloadforce-download

In php Instead of downloading csv file it gets open in the browser


I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.

Here is what i tried so far:

if(isset($currency)) {
    header("Content-Type: application/csv");
    header("Content-Disposition: attachment;Filename=Pricelogs.csv");
    ob_clean();
    $filename = "/tmp/".uniqid().".csv";
    exportCSVFile($filename, $currency, $country, $provider);
    readfile($filename);
    //unlink("'".$filename."'");
} else {
    echo "ERR_USERNAME_PASSWORD";
    exit();
}

I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes. I also tried header("Content-Type: text/csv"); Also:

    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=\"".$filename."\";" );
    header("Content-Transfer-Encoding: binary");

Solution

  • PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.

    http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv