Http::download PHP Method

download() public static method

+---------------------------------------------------------- 下载文件 可以指定下载显示的文件名,并自动发送相应的Header信息 如果指定了content参数,则下载该参数的内容 +----------------------------------------------------------
public static download ( string $filename, string $showname = '', string $content = '', integer $expire = 180 )
$filename string 下载文件名
$showname string 下载显示的文件名
$content string 下载的内容
$expire integer 下载内容浏览器缓存时间 +---------------------------------------------------------- +----------------------------------------------------------
    public static function download($filename, $showname = '', $content = '', $expire = 180)
    {
        if (is_file($filename)) {
            $length = filesize($filename);
        } elseif (is_file(UPLOAD_PATH . $filename)) {
            $filename = UPLOAD_PATH . $filename;
            $length = filesize($filename);
        } elseif ($content != '') {
            $length = strlen($content);
        } else {
            throw_exception(L('PUBLIC_ATTACH_ISNULL'));
            //throw_exception($filename.L('PUBLIC_ATTACH_ISNULL'));
        }
        if (empty($showname)) {
            $showname = $filename;
        }
        //造成部分中文文件的乱码问题
        //$showname = basename($showname);
        if (!empty($filename)) {
            $type = mime_content_type($filename);
        } else {
            $type = 'application/octet-stream';
        }
        //发送Http Header信息 开始下载
        header('Pragma: public');
        header('Cache-control: max-age=' . $expire);
        //header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire) . 'GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . 'GMT');
        header('Content-Length: ' . $length);
        header('Content-type: ' . $type);
        header('Content-Disposition: attachment; filename= ' . $showname . ' ');
        header('Content-Encoding: none');
        header('Content-Transfer-Encoding: binary');
        if ($content == '') {
            readfile($filename);
        } else {
            echo $content;
        }
        exit;
    }

Usage Example

示例#1
0
 public function file_download()
 {
     $uploadpath = './Uploads/file/';
     //设置文件上传路径,服务器上的绝对路径
     $id = $_GET['id'];
     //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
     if ($id == '') {
         //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。
         $this->display('没有找到该文件', U('Index/Index/index'));
     }
     $file = M('file');
     //利用与表file对应的数据模型类FileModel来建立数据对象。
     $result = $file->find($id);
     //根据id查询到文件信息
     if ($result == false) {
         //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面。或可做其他处理
         $this->display('没有找到该文件', U('Index/Index/index'));
     }
     $savename = $file->file_savename;
     //文件保存名
     $showname = $file->file_truename;
     //文件原名
     $filename = $uploadpath . $savename;
     //完整文件名(路径加名字)
     //print_r($filename);die;
     import('ORG.Net.Http');
     Http::download($filename, $showname);
 }
All Usage Examples Of Http::download