PHP 下载功能源码

来源:互联网 发布:巨森网络 编辑:程序博客网 时间:2024/04/30 14:47

1、页面中 下载连接

             <a href="__URL__/down?file=D:\ABC&type=zip">下载文件</a>

2、控制器中down方法

        

             public function down(){
        
        if (!isset($_GET["file"]) || !isset($_GET["type"])) {
            print "no file selsect"; exit();
        }
        
        $file = $_GET["file"].".".$_GET["type"];

        if (@$fp = fopen($file,'r')){
        
            header ("Content-type: octet/stream");
        
            if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")){
        
                header("Content-Disposition: filename=ABC.jpeg".mb_convert_encoding('ABC.zip','GB2312','UTF-8')); // For IE
        
            }else{

                header("Content-Disposition: attachment; filename=".mb_convert_encoding('ABC.zip','GB2312','UTF-8')); // For Other browsers
        
            } while(!@feof($fp)){
        
                echo fread($fp,1024);
            }   
            exit();
        
        } else{
        
            print "此文件不存在";
        }
    }




0 0
原创粉丝点击