195 lines
4.0 KiB
PHP
195 lines
4.0 KiB
PHP
<?php
|
||
namespace Files;
|
||
|
||
class Output
|
||
{
|
||
|
||
private $db;//传入PDO对象
|
||
|
||
|
||
function __construct($db)
|
||
{
|
||
$this->db = $db;
|
||
}
|
||
|
||
//输出下载
|
||
public function Download($filename,$content,$filetype="JSON",$zfmvc=""){
|
||
|
||
if(empty($filename) || empty($content) || empty($filetype))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$filetype = strtolower($filetype);
|
||
|
||
//支持的输出类型
|
||
$functions = array(
|
||
"json"=>"JsonOutPut",
|
||
"xml"=>"XMLOutPut",
|
||
"csv"=>"CSVOutPut",
|
||
);
|
||
|
||
$file_type_functions = array();
|
||
foreach($functions as $k=>$v)
|
||
{
|
||
$file_type_functions[] = $k;
|
||
}
|
||
|
||
if(!in_array($filetype,$file_type_functions))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$output_body = $this->$functions[$filetype]($content);
|
||
|
||
$this->pushDownload($output_body,$filename,$filetype,$zfmvc);
|
||
|
||
}//Download
|
||
|
||
//输出JSON内容
|
||
public function JsonOutPut($content,$numeric = true){
|
||
|
||
if($numeric == true)
|
||
{
|
||
return json_encode($content,JSON_NUMERIC_CHECK);
|
||
}else{
|
||
return json_encode($content);
|
||
}
|
||
|
||
}//JsonOutPut()
|
||
|
||
//输出XML内容
|
||
public function XMLOutPut($content){
|
||
|
||
|
||
}//JsonOutPut()
|
||
|
||
//输出CSV内容
|
||
public function CSVOutPut($data,$head = ""){
|
||
|
||
$split = ",";
|
||
|
||
$content = "";
|
||
|
||
//如果是windows,输出一个BOM头
|
||
if(strpos($_SERVER["HTTP_USER_AGENT"],"Windows"))
|
||
{
|
||
$content = "\xEF\xBB\xBF";
|
||
}else{
|
||
$content = "";
|
||
}
|
||
|
||
if(!empty($head))
|
||
{
|
||
$content .= join($split,$head)."\r\n";
|
||
}
|
||
|
||
|
||
foreach($data as $k=>$v)
|
||
{
|
||
|
||
foreach($v as $kk=>$vv)
|
||
{
|
||
if(strpos($vv,","))
|
||
{
|
||
$v[$kk] = "\"".$vv."\"";
|
||
}
|
||
}
|
||
|
||
$content .= join($split,$v)."\r\n";
|
||
}
|
||
|
||
return $content;
|
||
}//JsonOutPut()
|
||
|
||
//输出下载
|
||
public function pushDownload($content,$filename,$type="",$zfmvc="")
|
||
{
|
||
if(headers_sent())
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(empty($filename) || empty($content))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$content_type = $this->ContentTypes($type);
|
||
$fsize = strlen($content);
|
||
|
||
//没有在zf controller 中使用
|
||
if(empty($zfmvc))
|
||
{
|
||
|
||
header('Content-Description: File Transfer');
|
||
header("Cache-Control: private",false);
|
||
header("Content-Type: ".$content_type);
|
||
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
|
||
header("Content-Transfer-Encoding: binary\n");
|
||
header("Expires: 0");
|
||
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
|
||
header("Pragma: public");
|
||
header("Content-Length: ".$fsize);
|
||
ob_clean();
|
||
flush();
|
||
|
||
echo $content;
|
||
|
||
exit();
|
||
}else{
|
||
$zfmvc->getResponse()->setHeader('Content-Type', $content_type)
|
||
->setHeader('Content-Disposition','attachment; filename="'.$filename.'"')
|
||
->setHeader('Content-Length', strlen($content))
|
||
->setHeader('Content-Type','application/force-download')
|
||
->setHeader('Content-Type','application/download')
|
||
->setHeader('Content-Description','File Transfer')
|
||
->setHeader('Content-Transfer-Encoding','binary')
|
||
->setHeader('Expires',0)
|
||
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
|
||
->setHeader('Pragma','public')
|
||
->setBody($content);
|
||
exit();
|
||
}
|
||
|
||
}//pushDownload
|
||
|
||
//Content-Type
|
||
public function ContentTypes($ext){
|
||
|
||
$ext = strtolower($ext);
|
||
|
||
$def = "application/force-download";
|
||
|
||
if(empty($ext))
|
||
{
|
||
return $def;
|
||
}
|
||
|
||
$content_types = array(
|
||
"csv" => "text/csv",
|
||
"pdf"=>"application/pdf",
|
||
"exe"=>"application/octet-stream",
|
||
"gzip"=>"application/zip",
|
||
"doc"=>"application/msword",
|
||
"xls"=>"application/vnd.ms-excel",
|
||
"ppt"=>"application/vnd.ms-powerpoint",
|
||
"gif"=>"image/gif",
|
||
"png"=>"image/png",
|
||
"jpeg"=>"image/jpg",
|
||
"jpg"=>"image/jpg",
|
||
"xml"=>"text/xml",
|
||
"xsl"=>"text/xml"
|
||
);
|
||
|
||
if(!isset($content_types[$ext]))
|
||
{
|
||
return $def;
|
||
}else{
|
||
return $content_types[$ext];
|
||
}
|
||
|
||
}//ContentTypes
|
||
|
||
}
|