加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 下载csv文件

发布时间:2020-12-13 22:16:05 所属栏目:PHP教程 来源:网络整理
导读:当我们点击该按钮时,我们有一个按钮,所有数据都进入csv文件并下载此csv文件. csv文件创建但下载代码不起作用 $fp = fopen("filecustomer-list.csv","w");fileName = "filecustomer-list.csv";$filePath = "filecustomer-list.csv";$fsize = filesize("fil
当我们点击该按钮时,我们有一个按钮,所有数据都进入csv文件并下载此csv文件.
csv文件创建但下载代码不起作用

$fp = fopen("filecustomer-list.csv","w");
fileName = "filecustomer-list.csv";
$filePath = "filecustomer-list.csv";
$fsize = filesize("filecustomer-list.csv");

if(($_POST['csv_download_list']) == "cm")
{
    fwrite($fp,$csv);
    fclose($fp); 
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename="$fileName"");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
    header('Content-Length: ' . filesize($filePath));
    ob_clean();
    flush();
    $file = @fopen($filePath,"rb");
    if ($file) {
        while(!feof($file)) {
            print(fread($file,1024*8));
            flush();
        }
    @fclose($file);
}
exit;

解决方法

使用此代码段应该执行您要执行的操作.

<?php

    $file = 'sample.csv'; //path to the file on disk

    if (file_exists($file)) {

        //set appropriate headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();

        //read the file from disk and output the content.
        readfile($file);
        exit;
    }
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读