PHP 浏览器下载文件

function export_text()
{

    $xlsName_1 = '123.txt'; //文件名
    $xlsName_2 = 'uploads/files/excel/'; //相对路径存放

    $xlsName = $xlsName_2 . $xlsName_1; //全路径

    if (file_exists($xlsName_2)) { //存在文件清空文件夹下的文件

        $dh = opendir($xlsName_2);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $xlsName_2 . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    deldir($fullpath);
                }
            }
        }
    } else {
        mkdir($xlsName_2, 0777, true); //不存在文件则创建
    }

    $data_list = PhoneModel::SelectList($field = 'user_phone'); //查询数据

    if (!empty($data_list)) {
        foreach ($data_list as $key => &$val) {

            $myfile = fopen($xlsName, "a");
            fwrite($myfile, $val['user_phone']); //写入文件把数据写入对应文件
            fwrite($myfile, "\r\n"); //写入换行
        }
    }

    //查询文件是否存在
    if (file_exists($xlsName)) {

        ob_start();
        $filename = $xlsName;
        header("Content-type:  application/octet-stream ");
        header("Accept-Ranges:  bytes ");
        header("Content-Disposition:  attachment;  filename= {$xlsName}");
        $size = readfile($filename);
        header("Accept-Length: " . $size);

    } else {

        return $this->error('没有数据!');
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值