Forcing to download a file using PHP

前端 未结 10 950
借酒劲吻你
借酒劲吻你 2020-11-22 04:04

I have a CSV file on my server. If a user clicks on a link it should download, but instead it opens up in my browser window.

My code looks as follows



        
10条回答
  •  名媛妹妹
    2020-11-22 04:24

    .htaccess Solution

    To brute force all CSV files on your server to download, add in your .htaccess file:

    AddType application/octet-stream csv
    

    PHP Solution

    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename=example.csv');
    header('Pragma: no-cache');
    readfile("/path/to/yourfile.csv");
    

提交回复
热议问题