增加了有命名空间和事件驱动模式的新文件模块
This commit is contained in:
parent
1353d1753c
commit
f217f41059
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
namespace Files\Event;
|
||||
|
||||
interface FileEvent
|
||||
{
|
||||
|
||||
public function checkExt(\Zend_EventManager_Event $e);
|
||||
|
||||
public function checkSize(\Zend_EventManager_Event $e);
|
||||
|
||||
public function makeThumb(\Zend_EventManager_Event $e);
|
||||
|
||||
}
|
|
@ -0,0 +1,385 @@
|
|||
<?php
|
||||
namespace Files;
|
||||
|
||||
use Files\Thumbnail;
|
||||
|
||||
class Files{
|
||||
|
||||
public $tbl_att = "tbl_attachments";
|
||||
public $db;
|
||||
public $source = "";
|
||||
protected $events = NULL; //事件
|
||||
|
||||
|
||||
function __construct($db = NULL)
|
||||
{
|
||||
if(empty($db))
|
||||
{
|
||||
$this->db = \Zend_Registry::get('db');
|
||||
}else{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
$this->config = \Zend_Registry::get('config');
|
||||
|
||||
$this->event = new \Zend_EventManager_EventManager();
|
||||
}
|
||||
|
||||
public function events(\Zend_EventManager_EventCollection $events = NULL)
|
||||
{
|
||||
if ($events !== NULL) {
|
||||
$this->events = $events;
|
||||
} elseif ($this->events === NULL) {
|
||||
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
|
||||
}
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
//上传申请表
|
||||
public function uploadApplicationForm($file,$orderid)
|
||||
{
|
||||
try{
|
||||
if (empty($file) !== false) {
|
||||
return array("error"=>"请选择要上传的文件");
|
||||
}
|
||||
|
||||
if (@is_uploaded_file($file['tmp_name']) === false) {
|
||||
return array("error"=>"文件上传失败,请重新上传");
|
||||
}
|
||||
|
||||
$ext = $this->getFileTextExt($file['name']);
|
||||
$filename = $orderid.".".$ext;
|
||||
|
||||
$dir = "../data/application_form/";
|
||||
|
||||
if (!file_exists($dir)) {
|
||||
mkdir($dir);
|
||||
}
|
||||
|
||||
$dir.=date("Y")."/";
|
||||
|
||||
if (!file_exists($dir)) {
|
||||
mkdir($dir);
|
||||
}
|
||||
|
||||
$new_filepath = $dir.$filename;
|
||||
|
||||
if (move_uploaded_file($file['tmp_name'], $new_filepath) === false) {
|
||||
return array("error"=>"上传失败,请重试");
|
||||
}
|
||||
|
||||
return array("file"=>$new_filepath);
|
||||
|
||||
}catch(Exception $e)
|
||||
{
|
||||
return array("error"=>$e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* upload
|
||||
*
|
||||
* 文件上传
|
||||
*
|
||||
* @param Array $files e.g. $_FILES['Filedata']
|
||||
*
|
||||
* return Array $msg e.g. if($msg['error'])
|
||||
*/
|
||||
public function upload($files,$dir = "",$filename="",$makeThumb = false)
|
||||
{
|
||||
if (empty($files) !== false) {
|
||||
return array("error"=>"请选择要上传的文件");
|
||||
}
|
||||
|
||||
if (@is_uploaded_file($files['tmp_name']) === false) {
|
||||
return array("error"=>"文件上传失败,请重新上传");
|
||||
}
|
||||
|
||||
$conf = new \Zend_Config_Ini("../application/conf/file.ini", "files");
|
||||
|
||||
$source = $this->source;
|
||||
$file = $files;
|
||||
|
||||
$results = $this->events()->trigger('upload.checkExt', $this, compact('source','file','conf'));
|
||||
$cache_data = $results->bottom();
|
||||
|
||||
if($cache_data !== true)
|
||||
{
|
||||
return $cache_data;
|
||||
}
|
||||
|
||||
$results = $this->events()->trigger('upload.checkSize', $this, compact('file','conf'));
|
||||
$cache_data = $results->bottom();
|
||||
|
||||
if($cache_data !== true)
|
||||
{
|
||||
return $cache_data;
|
||||
}
|
||||
|
||||
$msg = array();
|
||||
|
||||
$file_name = $files['name']; //原文件名
|
||||
$file_size = $files['size']; //文件大小
|
||||
|
||||
$results = $this->makeUploadTarget($conf);
|
||||
|
||||
if(isset($results['error']))
|
||||
{
|
||||
return $results;
|
||||
}//文件夹问题
|
||||
|
||||
$new_file_basename = $this->gen_uuid();
|
||||
$file_ext = $this->getFileTextExt($file_name);
|
||||
$new_file_name = $new_file_basename . '.' . $file_ext;//新文件名
|
||||
|
||||
//移动文件
|
||||
$file_path = $results['save_path'] . $new_file_name ;
|
||||
|
||||
if (move_uploaded_file($file['tmp_name'], $file_path) === false) {
|
||||
return array("error"=>"上传失败,请重试");
|
||||
}
|
||||
|
||||
$dbsave = $db_path = $results['dbsave'];
|
||||
$dbsave .= $new_file_name;//数据库最终存储的文件
|
||||
$file_url = $dbsave;//文件链接
|
||||
|
||||
$results = $this->events()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename'));
|
||||
$thumbnail = $results->bottom();
|
||||
|
||||
$msg['file_url'] = $file_url;
|
||||
$msg['file_size'] = $file_size;
|
||||
$msg['db_path'] = $conf->upload . $dbsave;
|
||||
$msg['realname'] = $file_name;
|
||||
$msg['file_ext'] = $file_ext;
|
||||
$msg['file_type'] = $this->getFileMime($file_path);
|
||||
$msg['thumb'] = $thumbnail;
|
||||
|
||||
return $msg;
|
||||
}//文件上传
|
||||
|
||||
//生成上传文件的地址
|
||||
public function makeUploadTarget($conf)
|
||||
{
|
||||
//文件保存目录路径
|
||||
$save_path = $conf->upload;
|
||||
|
||||
if (@is_dir($save_path) === false) {
|
||||
return array("error"=>"上传目录不存在。请联系管理员");
|
||||
}
|
||||
|
||||
if (@is_writable($save_path) === false) {
|
||||
return array("error"=>"上传目录没有写权限。请联系管理员");
|
||||
}
|
||||
|
||||
$dbsave = ""; //数据库中存放的路径
|
||||
|
||||
$y = date("Y");
|
||||
$m = date("m");
|
||||
$d = date("d");
|
||||
|
||||
$save_path .= $y . "/";
|
||||
$dbsave .= $y.'/';
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path);
|
||||
}
|
||||
|
||||
$save_path .= $m . "/";
|
||||
$dbsave .= $m.'/';
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path);
|
||||
}
|
||||
|
||||
$save_path .= $d . "/";
|
||||
$dbsave .= $d.'/';
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path);
|
||||
}
|
||||
|
||||
return array("save_path"=>$save_path,"dbsave"=>$dbsave);
|
||||
}//创建上传文件的地址
|
||||
|
||||
//获取文件扩展名
|
||||
public function getFileTextExt($file_name)
|
||||
{
|
||||
$temp_arr = explode(".", $file_name);
|
||||
$file_ext = array_pop($temp_arr);
|
||||
$file_ext = trim($file_ext);
|
||||
$file_ext = strtolower($file_ext);
|
||||
return $file_ext;
|
||||
}
|
||||
|
||||
//获取文件Mime,通过finfo扩展
|
||||
public function getFileMime($file_name)
|
||||
{
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$filetype = finfo_file($finfo, $file_name) ; //文件mime类型
|
||||
finfo_close($finfo);
|
||||
return $filetype;
|
||||
}
|
||||
|
||||
//文件名uuid
|
||||
public function gen_uuid() {
|
||||
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
// 32 bits for "time_low"
|
||||
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
|
||||
|
||||
// 16 bits for "time_mid"
|
||||
mt_rand( 0, 0xffff ),
|
||||
|
||||
// 16 bits for "time_hi_and_version",
|
||||
// four most significant bits holds version number 4
|
||||
mt_rand( 0, 0x0fff ) | 0x4000,
|
||||
|
||||
// 16 bits, 8 bits for "clk_seq_hi_res",
|
||||
// 8 bits for "clk_seq_low",
|
||||
// two most significant bits holds zero and one for variant DCE1.1
|
||||
mt_rand( 0, 0x3fff ) | 0x8000,
|
||||
|
||||
// 48 bits for "node"
|
||||
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* getOne()
|
||||
*
|
||||
* 从数据库获取单个文件的信息
|
||||
*
|
||||
* @param int id
|
||||
*
|
||||
* return array
|
||||
*/
|
||||
public function getOne($id){
|
||||
$db = $this->db;
|
||||
//下载单个附件
|
||||
$sql = $db->query("select a.* from ".$this->tbl_att." a where a.id=$id order by a.ts_created desc");
|
||||
$att = $sql->fetch();
|
||||
if(empty($att['id']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return $att;
|
||||
}
|
||||
|
||||
public function delete($id){
|
||||
$att = $this->getOne($id);
|
||||
if(!empty($att['thumb']))
|
||||
{
|
||||
$thumbs = json_decode($att['thumb'],true);
|
||||
if(count($thumbs)>0)
|
||||
{
|
||||
foreach($thumbs as $v)
|
||||
{
|
||||
@unlink($v['file']);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(file_exists($att['filename'])){
|
||||
if(unlink($att['filename']))
|
||||
{
|
||||
$sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id";
|
||||
return $this->db->exec($sql);
|
||||
}else{
|
||||
return "删除失败";
|
||||
}
|
||||
}else{
|
||||
$sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id";
|
||||
if($this->db->exec($sql))
|
||||
{
|
||||
return "文件不存在,数据库记录删除成功";
|
||||
}else{
|
||||
return "文件不存在,数据库记录删除失败";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getAll($fields = ""){
|
||||
|
||||
$wheresql = array();
|
||||
if($fields!="")
|
||||
{
|
||||
$wheresql[] = " a.id IN ($fields) ";
|
||||
}
|
||||
if(count($wheresql)>0)
|
||||
{
|
||||
$wheresql = " WHERE ".join(",",$wheresql);
|
||||
}else{
|
||||
$wheresql = "";
|
||||
}
|
||||
|
||||
$sql = "SELECT a.*,u.username,u.id as uid FROM ".$this->tbl_att." a
|
||||
LEFT JOIN tbl_member u ON a.userid=u.id
|
||||
$wheresql
|
||||
ORDER BY a.id DESC";
|
||||
$rs = $this->db->query($sql);
|
||||
$rows = $rs->fetchAll();
|
||||
return $rows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* downloadFile
|
||||
*
|
||||
* 文件下载
|
||||
*
|
||||
* @param String $fullPath e.g. Zend_Registry::get('upload').$fileurl
|
||||
*
|
||||
* return file
|
||||
*/
|
||||
static function downloadFile( $fullPath ){
|
||||
|
||||
//判断是否已经输出头部
|
||||
if( headers_sent() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
|
||||
// Required for some browsers
|
||||
@apache_setenv('no-gzip', 1);
|
||||
@ini_set('zlib.output_compression', 0);
|
||||
|
||||
// File Exists?
|
||||
if( file_exists($fullPath) ){
|
||||
|
||||
// Parse Info / Get Extension
|
||||
$fsize = filesize($fullPath);
|
||||
$path_parts = pathinfo($fullPath);
|
||||
$ext = strtolower($path_parts["extension"]);
|
||||
|
||||
// Determine Content Type
|
||||
switch ($ext) {
|
||||
case "pdf": $ctype="application/pdf"; break;
|
||||
case "exe": $ctype="application/octet-stream"; break;
|
||||
case "zip": $ctype="application/zip"; break;
|
||||
case "doc": $ctype="application/msword"; break;
|
||||
case "xls": $ctype="application/vnd.ms-excel"; break;
|
||||
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
|
||||
case "gif": $ctype="image/gif"; break;
|
||||
case "png": $ctype="image/png"; break;
|
||||
case "jpeg":
|
||||
case "jpg": $ctype="image/jpg"; break;
|
||||
default: $ctype="application/force-download";
|
||||
}
|
||||
|
||||
header('Content-Description: File Transfer');
|
||||
header("Cache-Control: private",false);
|
||||
header("Content-Type: $ctype");
|
||||
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
|
||||
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();
|
||||
if(readfile($fullPath))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
} else
|
||||
{return false;}
|
||||
}
|
||||
} //文件下载
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
namespace Files\Listener;
|
||||
|
||||
use Files\Operation\FileOperate;
|
||||
|
||||
class FileListener implements \Zend_EventManager_ListenerAggregate
|
||||
{
|
||||
private $event;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->event = new \Zend_EventManager_EventManager();
|
||||
}
|
||||
|
||||
public function attach(\Zend_EventManager_EventCollection $events)
|
||||
{
|
||||
$_Events = new FileOperate();
|
||||
$events->attach('upload.checkExt', array($_Events, 'checkExt'), 100);
|
||||
$events->attach('upload.checkSize', array($_Events, 'checkSize'), 80);
|
||||
$events->attach('upload.makeThumb', array($_Events, 'makeThumb'), 70);
|
||||
}
|
||||
|
||||
public function detach(\Zend_EventManager_EventCollection $events)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
namespace Files\Operation;
|
||||
|
||||
use Files\Thumbnail;
|
||||
|
||||
class FileOperate implements \Files\Event\FileEvent
|
||||
{
|
||||
private $db; //传入PDO对象
|
||||
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式,防止出现sdtClass错误
|
||||
private $config; //全局配置
|
||||
|
||||
|
||||
public $source;
|
||||
public $allow_source = array(
|
||||
'voiceimage', //声音投稿的图片
|
||||
'voice' //音频文件
|
||||
);
|
||||
public $def_allow_ext = array(
|
||||
'gif', 'jpg', 'jpeg', 'png', 'bmp',
|
||||
'flv','swf', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb',
|
||||
'pdf','doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'
|
||||
);
|
||||
|
||||
function __construct($db = NULL)
|
||||
{
|
||||
if(empty($db))
|
||||
{
|
||||
$this->db = \Zend_Registry::get('db');
|
||||
}else{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
$this->config = \Zend_Registry::get('config');
|
||||
$this->source = $this->loadFileSource();
|
||||
}
|
||||
|
||||
private function loadFileSource()
|
||||
{
|
||||
return array(
|
||||
'voiceimage'=> array('gif','jpg','jpeg','png'),
|
||||
'voice'=>array('audio/mpeg')
|
||||
);
|
||||
}
|
||||
|
||||
public function checkExt(\Zend_EventManager_Event $e){
|
||||
$source = (string)$e->getParam('source');
|
||||
$file = $e->getParam('file');
|
||||
$conf = $e->getParam('conf');
|
||||
|
||||
if(empty($source))
|
||||
{
|
||||
//定义允许上传的文件扩展名
|
||||
if(isset($conf->ext) && !empty($conf->ext))
|
||||
{
|
||||
$ext_arr = explode(",",$conf->ext);
|
||||
}else{
|
||||
$ext_arr = $this->def_allow_ext;
|
||||
}
|
||||
}else{
|
||||
if(!in_array($source,$this->allow_source)){
|
||||
return array("error"=>'错误的来源');
|
||||
}else{
|
||||
$ext_arr = $this->source[$source];
|
||||
}
|
||||
}
|
||||
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$filetype = finfo_file($finfo, $file['tmp_name']) ; //文件mime类型
|
||||
finfo_close($finfo);
|
||||
|
||||
foreach($ext_arr as $v)
|
||||
{
|
||||
if(strpos($filetype,$v) !== false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return array("error"=>"您上传的文件不在允许的范围内".$filetype." was not in ".join(",",$ext_arr));
|
||||
}//checkParam
|
||||
|
||||
public function checkSize(\Zend_EventManager_Event $e){
|
||||
$file = $e->getParam('file');
|
||||
|
||||
$file_size = $file['size'];
|
||||
|
||||
if(!isset($conf->maxsize) && !empty($conf->maxsize))
|
||||
{
|
||||
$max_size = (int)$conf->maxsize;
|
||||
}else{
|
||||
$max_size = 10*1024*1024;
|
||||
}
|
||||
|
||||
if($file_size > $max_size)
|
||||
{
|
||||
return array("error"=>'上传的文件超过了允许的文件大小');
|
||||
}
|
||||
|
||||
return true;
|
||||
}//checkSize
|
||||
|
||||
public function makeThumb(\Zend_EventManager_Event $e)
|
||||
{
|
||||
$file_path = $e->getParam('file_path');
|
||||
$conf = $e->getParam('conf');
|
||||
$save_path = $conf->upload;
|
||||
$dbsave = $e->getParam('db_path');
|
||||
$file_ext = $e->getParam('file_ext');
|
||||
$new_file_basename = $e->getParam('new_file_basename');
|
||||
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$filetype = finfo_file($finfo, $file_path) ; //文件mime类型
|
||||
finfo_close($finfo);
|
||||
|
||||
$thumb_conf = new \Zend_Config_Ini("../application/conf/thumb.ini", "thumb");
|
||||
|
||||
if($thumb_conf->makethumb != "on")
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
$allowmime = $thumb_conf->imagemime;
|
||||
if(empty($allowmime))
|
||||
{
|
||||
$allowmime = array('image/jpeg','image/png','image/gif');
|
||||
}else{
|
||||
$allowmime = explode(",",$allowmime);
|
||||
}
|
||||
|
||||
if(!in_array($filetype,$allowmime))
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
$thumb_size = explode(",",$thumb_conf->thumbsize);
|
||||
|
||||
$thumbnail = array();
|
||||
|
||||
foreach($thumb_size as $v)
|
||||
{
|
||||
$v = (int)$v;
|
||||
$thumb = new Thumbnail($v, $v);
|
||||
$image = file_get_contents($file_path);
|
||||
$thumb->loadData($image, $filetype);
|
||||
$thumbfile = $save_path.$dbsave.$new_file_basename.'_'.$v. '.' .$file_ext;
|
||||
$thumbnail[$v] = array(
|
||||
'size' => $v,
|
||||
'ext' => $file_ext,
|
||||
'file' => $thumbfile,
|
||||
'url' => $dbsave.$new_file_basename.'_'.$v. '.' .$file_ext ,
|
||||
);
|
||||
$thumb->buildThumb($thumbfile);
|
||||
}
|
||||
|
||||
return $thumbnail;
|
||||
|
||||
}//makeThumb
|
||||
}
|
|
@ -0,0 +1,194 @@
|
|||
<?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
|
||||
|
||||
}
|
|
@ -0,0 +1,176 @@
|
|||
<?php
|
||||
namespace Files;
|
||||
/**
|
||||
* 生成缩略图(支持加载图片文件和字符串2种方式)
|
||||
* @param $maxWidth 缩略图最大宽度
|
||||
* @param $maxHeight 缩略图最大高度
|
||||
* @param bool $scale 是否按比例缩小,否则拉伸
|
||||
* @param bool $inflate 是否放大以来填充缩略图
|
||||
*/
|
||||
class Thumbnail {
|
||||
|
||||
private $maxWidth;
|
||||
private $maxHeight;
|
||||
private $scale;
|
||||
private $inflate;
|
||||
private $types;
|
||||
private $imgLoaders;
|
||||
private $imgCreators;
|
||||
private $source;
|
||||
private $sourceWidth;
|
||||
private $sourceHeight;
|
||||
private $sourceMime;
|
||||
private $thumb;
|
||||
private $thumbWidth;
|
||||
private $thumbHeight;
|
||||
|
||||
public function __construct($maxWidth, $maxHeight, $scale = true, $inflate = false) {
|
||||
$this->maxWidth = $maxWidth;
|
||||
$this->maxHeight = $maxHeight;
|
||||
$this->scale = $scale;
|
||||
$this->inflate = $inflate;
|
||||
$this->types = array(
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/gif'
|
||||
);
|
||||
//加载MIME类型图像的函数名称
|
||||
$this->imgLoaders = array(
|
||||
'image/jpeg' => 'imagecreatefromjpeg',
|
||||
'image/png' => 'imagecreatefrompng',
|
||||
'image/gif' => 'imagecreatefromgif'
|
||||
);
|
||||
//储存创建MIME类型图片的函数名称
|
||||
$this->imgCreators = array(
|
||||
'image/jpeg' => 'imagejpeg',
|
||||
'image/png' => 'imagepng',
|
||||
'image/gif' => 'imagegif'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* 文件方式加载图片
|
||||
* @param string $image 源图片
|
||||
* @return bool
|
||||
*/
|
||||
public function loadFile($image){
|
||||
if(!$dims = @getimagesize($image)){
|
||||
trigger_error("源图片不存在");
|
||||
}
|
||||
if(in_array($dims['mime'], $this->types)){
|
||||
$loader = $this->imgLoaders[$dims['mime']];
|
||||
$this->source = $loader($image);
|
||||
if($dims['mime'] == 'image/png' || $dims['mime'] == 'image/gif'){
|
||||
imagesavealpha($this->source, true);
|
||||
}
|
||||
$this->sourceWidth = $dims[0];
|
||||
$this->sourceHeight = $dims[1];
|
||||
$this->sourceMime = $dims['mime'];
|
||||
$this->initThumb();
|
||||
return TRUE;
|
||||
}else{
|
||||
trigger_error('不支持'.$dims['mime']."图片类型");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 字符串方式加载图片
|
||||
* @param string $image 字符串
|
||||
* @param string $mime 图片类型
|
||||
* @return type
|
||||
*/
|
||||
public function loadData($image,$mime){
|
||||
if(in_array($mime, $this->types)){
|
||||
if($this->source = @imagecreatefromstring($image)){
|
||||
$this->sourceWidth = imagesx($this->source);
|
||||
$this->sourceHeight = imagesy($this->source);
|
||||
$this->sourceMime = $mime;
|
||||
$this->initThumb();
|
||||
return TRUE;
|
||||
}else{
|
||||
trigger_error("不能从字符串加载图片");
|
||||
}
|
||||
}else{
|
||||
trigger_error("不支持".$mime."图片格式");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 生成缩略图
|
||||
* @param string $file 文件名。如果不为空则储存为文件,否则直接输出到浏览器
|
||||
*/
|
||||
public function buildThumb($file = NULL){
|
||||
$creator = $this->imgCreators[$this->sourceMime];
|
||||
if(isset($file) && $this->thumb !== NULL){
|
||||
return $creator($this->thumb,$file);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 处理缩放
|
||||
*/
|
||||
public function initThumb(){
|
||||
if($this->scale){
|
||||
if($this->sourceWidth > $this->sourceHeight){
|
||||
$this->thumbWidth = $this->maxWidth;
|
||||
$this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));
|
||||
}elseif($this->sourceWidth < $this->sourceHeight){
|
||||
$this->thumbHeight = $this->maxHeight;
|
||||
$this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight));
|
||||
}else{
|
||||
$this->thumbWidth = $this->maxWidth;
|
||||
$this->thumbHeight = $this->maxHeight;
|
||||
}
|
||||
}
|
||||
|
||||
if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){
|
||||
$this->thumb = NULL;
|
||||
}else{
|
||||
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
|
||||
if($this->sourceMime == 'image/png' || $this->sourceMime == 'image/gif')
|
||||
{
|
||||
|
||||
imagealphablending($this->thumb, true);
|
||||
imagesavealpha($this->thumb, true);
|
||||
if($this->sourceMime == 'image/gif')
|
||||
{
|
||||
$bgcolor=imagecolorallocate($this->thumb,0,0,0);
|
||||
$transparent = imagecolortransparent($this->thumb,$bgcolor) ;
|
||||
}
|
||||
if($this->sourceMime == 'image/png')
|
||||
{
|
||||
$transparent = imagecolorallocatealpha($this->thumb, 0, 0, 0, 127);
|
||||
}
|
||||
imagefill($this->thumb, 0, 0, $transparent);
|
||||
}
|
||||
imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public function getMine(){
|
||||
return $this->sourceMime;
|
||||
}
|
||||
|
||||
public function getThumbWidth(){
|
||||
return $this->thumbWidth;
|
||||
}
|
||||
|
||||
public function getThumbHeight(){
|
||||
return $this->thumbHeight;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩略图类调用示例(文件)
|
||||
|
||||
$thumb = new Thumbnail(200, 200);
|
||||
$thumb->loadFile('wap.gif');
|
||||
header('Content-Type:'.$thumb->getMine());
|
||||
$thumb->buildThumb();
|
||||
/**
|
||||
* 缩略图类调用示例(字符串)
|
||||
|
||||
$thumb = new Thumbnail(200, 200);
|
||||
$image = file_get_contents('wap.gif');
|
||||
$thumb->loadData($image, 'image/jpeg');
|
||||
$thumb->buildThumb('wap_thumb.gif');
|
||||
*/
|
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
namespace Helper;
|
||||
|
||||
class View extends \Zend_Controller_Plugin_Abstract
|
||||
{
|
||||
private $db; //传入PDO对象.
|
||||
private $product = 0; //产品环境
|
||||
|
||||
function __construct($db = NULL)
|
||||
{
|
||||
if(empty($db))
|
||||
{
|
||||
$this->db = \Zend_Registry::get('db');
|
||||
}else{
|
||||
$this->db = $db;
|
||||
}
|
||||
}
|
||||
|
||||
static function addPaginator($data,$ctl,$limit = 10)
|
||||
{
|
||||
$request = $ctl->getRequest();
|
||||
$page = $request->getParam('page');
|
||||
|
||||
$paginator = \Zend_Paginator::factory($data);
|
||||
$paginator->setCurrentPageNumber($page);
|
||||
$paginator->setItemCountPerPage($limit);
|
||||
$paginator->setView($ctl->view);
|
||||
\Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
|
||||
$ctl->view->paginator = $paginator;
|
||||
}
|
||||
|
||||
static function Msg($type,$content,$url=''){
|
||||
$html = '<div class="alert '.$type.'">'."\r\n";
|
||||
$html.= '<a data-dismiss="alert" class="close">×</a>'."\r\n";
|
||||
$html.= $content."\r\n";
|
||||
$html.= '</div>'."\r\n";
|
||||
if(!empty($url))
|
||||
{
|
||||
if($url == -1){
|
||||
$html.= '<script language="javascript">setTimeout("window.history.back(-1);",3000);</script>'."\r\n";
|
||||
}else{
|
||||
$html.= '<script language="javascript">setTimeout("self.location=\''.$url.'\'",3000);</script>'."\r\n";
|
||||
}
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
static function Error($content,$type='',$url=''){
|
||||
if(empty($type))
|
||||
{
|
||||
$AlertType = "alert-error";
|
||||
}else{
|
||||
$AlertType = $type;
|
||||
}
|
||||
$html = '<div class="alert alert-block fade in '.$AlertType.'" id="Alert-error-box">'."\r\n";
|
||||
$html.= '<a class="close" data-dismiss="alert" href="#">×</a>'."\r\n";
|
||||
if(!is_array($content)) {
|
||||
$html.= '<h4 class="alert-heading">'.$content.'</h4>'."\r\n";
|
||||
}else{
|
||||
$html.= '<ul>'."\r\n";
|
||||
foreach($content as $v) {
|
||||
$html.='<li>'.$v.'</li>'."\r\n";
|
||||
}
|
||||
$html.= '</ul>'."\r\n";
|
||||
}
|
||||
$html.= '</div>'."\r\n";
|
||||
return $html;
|
||||
}
|
||||
|
||||
static function User($param = NULL){
|
||||
$auth = \Zend_Auth::getInstance();
|
||||
if($auth->hasIdentity())
|
||||
{
|
||||
if(!empty($param))
|
||||
{
|
||||
$user = $auth->getIdentity();
|
||||
return $user->$param;
|
||||
}else{
|
||||
$user = $auth->getIdentity();
|
||||
return $user;
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static function Dump($data,$exit = true){
|
||||
echo "<pre>";
|
||||
var_dump($data);
|
||||
echo "</pre>";
|
||||
if($exit)
|
||||
{
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
static function Post(\Zend_Controller_Action $ctl,$msg,$url=""){
|
||||
|
||||
if(empty($msg))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(!is_array($msg))
|
||||
{
|
||||
$msg = array('content'=>$msg,'url'=>$url);
|
||||
}
|
||||
|
||||
$helper = new \Zend_Controller_Action_HelperBroker($ctl);
|
||||
$helper->viewRenderer->setNoRender();
|
||||
echo $ctl->view->partial('post-message.phtml', $msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
static function HttpError($ctl,$code = 404){
|
||||
$ctl->getResponse()->setHttpResponseCode($code);
|
||||
$helper = new \Zend_Controller_Action_HelperBroker($ctl);
|
||||
$helper->layout->setLayout('layout');
|
||||
$helper->viewRenderer->setNoRender();
|
||||
echo $ctl->view->partial('error/404.phtml');
|
||||
return true;
|
||||
}
|
||||
|
||||
static function getHostLink()
|
||||
{
|
||||
$protocol = "http";
|
||||
if(strpos(strtolower($_SERVER['SERVER_PROTOCOL']),"https"))
|
||||
{
|
||||
$protocol = "https";
|
||||
}
|
||||
return $protocol."://".$_SERVER['SERVER_NAME'];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue