In php Instead of loading the csv file it opens in a browser

I am trying to download a CSV file through a browser. The script partially works, because so far I have been able to display the CSV on the screen, but the download does not start.

Here is what I have 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 already read all the questions of this type from the FAQ, and also tried, but it opens only in the browser, and not for downloading.
I also used a single quote header. 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");
+1
source share
4 answers

Paste the URL of your CSV file, it will be displayed in the browser instead of the downloadable browser force. You can open it in an iframe on your website.

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

+1

:

header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");

// print CSV lines here

exit();
+1

I can say that this combination works for me:

$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;

If I were you, I would first check it explicitly (outside of all your buffering), first make sure that you will be able to complete this work, and only then check it in your specific setting.

0
source

You can try this.

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );

print $content;
0
source

Source: https://habr.com/ru/post/1655922/


All Articles