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

后端 未结 4 633
梦毁少年i
梦毁少年i 2021-01-16 09:33

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 start

相关标签:
4条回答
  • 2021-01-16 09:38

    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

    0 讨论(0)
  • 2021-01-16 09:42

    I can tell you this combination is working 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 test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.

    0 讨论(0)
  • 2021-01-16 09:56

    I usually just do:

    header('Content-type: text/csv');
    header("Content-Disposition: attachment; filename=my_csv_filename.csv");
    
    // print CSV lines here
    
    exit();
    
    0 讨论(0)
  • 2021-01-16 09:57

    You might 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 讨论(0)
提交回复
热议问题