Cml\Vendor\Http::download PHP Method

download() public static method

下载文件 可以指定下载显示的文件名,并自动发送相应的Header信息 如果指定了content参数,则下载该参数的内容
public static download ( string $filename, string $showname = '', string $content = '', integer $expire = 180 ) : void
$filename string 下载文件名
$showname string 下载显示的文件名
$content string 下载的内容
$expire integer 下载内容浏览器缓存时间
return void
    public static function download($filename, $showname = '', $content = '', $expire = 180)
    {
        $uploadPath = CML_PROJECT_PATH . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'Uploads' . DIRECTORY_SEPARATOR;
        if (is_file($filename)) {
            $length = filesize($filename);
        } elseif (is_file($uploadPath . $filename)) {
            $filename = $uploadPath . $filename;
            $length = filesize($filename);
        } elseif ($content != '') {
            $length = strlen($content);
        } else {
            throw new FileCanNotReadableException($filename . '下载文件不存在!');
        }
        if (empty($showname)) {
            $showname = $filename;
        }
        $showname = basename($showname);
        if (!empty($filename)) {
            $type = self::mimeContentType($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", Cml::$nowTime + $expire) . "GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s", Cml::$nowTime) . "GMT");
        header("Content-Disposition: attachment; filename=" . $showname);
        header("Content-Length: " . $length);
        header("Content-type: " . $type);
        header('Content-Encoding: none');
        header("Content-Transfer-Encoding: binary");
        if ($content == '') {
            readfile($filename);
        } else {
            echo $content;
        }
        exit;
    }