zoukankan      html  css  js  c++  java
  • php文件下载

    <?php
    /**
     * 文件下载
     * @param  string $file_name    文件名
     * @param  string $file_sub_dir 文件子路径
     * @return void               
     */
    function down_file($file_name,$file_sub_dir){
    
        //1、将文件名转码
        $file_name = iconv('utf-8', 'gb2312', $file_name);
    
        //2、拼接文件绝对路径
        $file_path = $_SERVER['DOCUMENT_ROOT'].$file_sub_dir.$file_name;
    
        //3、判断文件是否存在
        if(!file_exists($file_path)){
            echo '文件不存在';
            return ;
        }
    
        //4、打开文件
        $fp = fopen($file_path, 'r');
    
        //5、获取文件大小
        $file_size = filesize($file_path);
    
        //6、设置响应头
        header('Content-Type: application/octet-stream');
        header('Accept-Ranges: bytes');
        header('Content-Length: '.$file_size);
        header('Content-Disposition: attachment; filename='.$file_name);
    
        //7、返回数据
        $buffer = 1024;//定义每次写给客户端的大小,字节为单位
        while(!feof($fp)){
            $file_data = fread($fp, $buffer);
            echo $file_data;
        }
    
        //8、关闭文件
        fclose($fp);
    }
    
    
    //测试
    down_file('01-nginx介绍及编译安装.wmv','/test/file/');
    
  • 相关阅读:
    进程、线程、协程
    python垃圾回收机制
    python变量存储和深浅拷贝
    Linux常用命令
    二叉树四种遍历,节点个数,深度
    装饰器
    ArrayList、Vector
    集合、Collection、迭代器、List
    卖票
    关于Thread和Runnable
  • 原文地址:https://www.cnblogs.com/cnsec/p/13406987.html
Copyright © 2011-2022 走看看