• PHP 弹出文件下载 原理 代码


    /**
     * @author      default7<default7@zbphp.com>
     * @description 演示PHP弹出下载的原理
     *
     * @param $file_name
     */
    function downFile($file_name)
    {
        $file_path = "/tmp/" . $file_name;
        $buffer = 102400; //一次返回102400个字节
        if (!file_exists($file_path)) {
            echo "<script type='text/javascript'> alert('对不起!该文件不存在或已被删除。'); </script>";
    
            return;
        }
        $fp = fopen($file_path, "r");
        $file_size = filesize($file_path);
        $file_data = '';
        while (!feof($fp)) {
            $file_data .= fread($fp, $buffer);
        }
        fclose($fp);
    
        //Begin writing headers
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type:application/octet-stream;");
        header("Accept-Ranges:bytes");
        header("Accept-Length:{$file_size}");
        header("Content-Disposition:attachment; filename={$file_name}");
        header("Content-Transfer-Encoding: binary");
        echo $file_data;
    }
    


  • 相关阅读:
    P1144 最短路计数
    P2966 [USACO09DEC]牛收费路径Cow Toll Paths
    P2419 [USACO08JAN]牛大赛Cow Contest
    P1462 通往奥格瑞玛的道路
    P1346 电车
    P1339 [USACO09OCT]热浪Heat Wave
    P1418 选点问题
    P1330 封锁阳光大学
    P1182 数列分段Section II
    P2661 信息传递
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7044398.html
Copyright © 2020-2023  润新知