zoukankan      html  css  js  c++  java
  • PHP文件下载方式

    <?php
    // 不能是中文目录,其实如果是 .rar, .zip 类的这些文件,直接连接就可以下载了!
    function download($file_dir,$file_name)
    //参数说明:
    //file_dir:文件所在目录
    //file_name:文件名
    {
        $file_dir = trim($file_dir);//去掉路径中多余的空格
        //得出要下载的文件的路径
        if($file_dir != '')
        {
            $file_path = $file_dir;
            if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
                $file_path .= '/';
            $file_path .= $file_name;
        }           
        else
            $file_path = $file_name;   
       
        //判断要下载的文件是否存在
        if(!file_exists($file_path))
        {
            echo '对不起,你要下载的文件不存在。';
            return false;
        }
        $file_size = filesize($file_path);

        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: $file_size");
        header("Content-Disposition: attachment; filename=".$file_name);
       
        $fp = fopen($file_path,"r");
        $buffer_size = 1024;
        $cur_pos = 0;
       
        while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
        {
            $buffer = fread($fp,$buffer_size);
            echo $buffer;
            $cur_pos += $buffer_size;
        }
       
        $buffer = fread($fp,$file_size-$cur_pos);
        echo $buffer;
        fclose($fp);
        return true;
    }


    download('', 'note.txt');
    ?>

  • 相关阅读:
    maven打包相关
    增强现实中的摄像机成像模型
    关于sql的exists
    oracle 连接之后出现 no logged on 问题
    关于oracle用户名登录
    myeclipse的SVN插件使用
    svn客户端使用
    svn服务器端使用
    SSH整合
    spring事务管理,xml配置aop事务和注解配置aop事务
  • 原文地址:https://www.cnblogs.com/lin3615/p/3543531.html
Copyright © 2011-2022 走看看