Ejemplo n.º 1
0
 /**
  * csv format
  * 0 开头是不留空,以行为单位。
  * 1 可含或不含列名,含列名则居文件第一行。
  * 2 一行数据不垮行,无空行。
  * 3 以半角符号,作分隔符,列为空也要表达其存在。
  * 4 列内容如存在半角逗号(即,)则用半角引号(即"")将该字段值包含起来。(ad中有可能出现逗号:"\""+ ad + "\"" )
  * 5 列内容如存在半角引号(即")则应替换成半角双引号("")转义。
  * 6 文件读写时引号,逗号操作规则互逆。
  * 7 内码格式不限,可为ASCII、Unicode或者其他。
  *
  * output csv format data
  * @param null $filename
  * @return bool
  */
 public function csv($filename = null)
 {
     $view = $this->initView();
     $template = $this->template(null, null, '.csv');
     $content = $view->render($template, true);
     $content = mb_convert_encoding($content, 'gbk', 'utf-8');
     if (empty($filename)) {
         $filename = Str::rand(8) . '.csv';
     }
     Response::download($filename);
     echo $content;
     return true;
 }
Ejemplo n.º 2
0
    public static function error($e)
    {
        $isException = is_object($e);
        if ($isException) {
            $code = $e->getCode();
            $message = $e->getMessage();
        } else {
            $code = $e;
        }
        switch ($code) {
            case 500:
                $message = 'Internal Server Error!';
                break;
            case 404:
                // $message = "Not Found! <!--$code-->";
                $code = 404;
                break;
            case 403:
                $message = 'Forbidden';
                break;
            default:
                $message = 'Error!';
        }
        @ob_end_clean();
        ob_start();
        header('Content-Type: text/html; charset=utf-8', true);
        if (is_numeric($code) && $code > 200) {
            Response::setStatus($code);
        }
        $message = nl2br($message);
        print <<<EOF
<!doctype html><html><head><meta charset="utf-8" />
<title>{$code}</title>
<meta http-equiv="refresh" content="5; url=/" />
<style type="text/css">
body{background: #f7fbe9;font-family: "Lucida Grande","Lucida Sans Unicode",Tahoma,Verdana;}
#error {background: #333;width: 360px;margin: 0 auto;margin-top: 100px;color: #fff;padding: 10px;
}
a{color:#fff;text-decoration:none}
h1 {padding: 10px;margin: 0;font-size: 36px;}
p {padding: 0 20px 20px 20px;margin: 0;font-size: 12px;}
img {padding: 0 0 5px 260px;}
</style>
</head>
<body>
<div id="error">
<h1>{$code}</h1>
<p>{$message}</p>
<p><a href="/">请访问首页</a></p>
</div>
</body>
</html>

EOF;
    }