php csv file as download

2019-08-17 09:48发布

i have a file called test.csv on my webserver and i like to download it:

<a href="test.csv">CSV Download</a>

But the file will be opend in a browser and not downloaded.

4条回答
淡お忘
2楼-- · 2019-08-17 10:28

Unless the client has some sort of "parser", you are unable to do it by default.

You will have to create your own php service or otherwise, to echo the contents of the file.

查看更多
劳资没心,怎么记你
3楼-- · 2019-08-17 10:33

I think you have to set the Content Type check http://php.net/manual/en/function.header.php

查看更多
Juvenile、少年°
4楼-- · 2019-08-17 10:34

Create an .htaccess file and write this in it:

AddType application/octet-stream .csv

This should tell apache to force the browser to download the file instead of open it.

查看更多
Viruses.
5楼-- · 2019-08-17 10:44

here is the code,

    $cfile = "path/to/filename.csv";
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename='filename.csv');
    @readfile($cfile);

put this one as a function and called this when you click the download.

查看更多
登录 后发表回答