修改Helpers\PDO调用大小写问题

This commit is contained in:
Li Jianxuan 2014-03-19 04:05:20 +00:00
parent 7d2bfb34fc
commit 90595c2e6a
2 changed files with 354 additions and 354 deletions

View File

@ -15,7 +15,7 @@ use Zend\View\Model\JsonModel;
use Zend\Http\PhpEnvironment\Request; use Zend\Http\PhpEnvironment\Request;
use Sookon\Helpers\View as view; use Sookon\Helpers\View as view;
use Sookon\Helpers\Dbh as dbh; use Sookon\Helpers\Dbh as dbh;
use Sookon\Helpers\Pdo; use Sookon\Helpers\PDO;
use Sookon\Helpers\Config; use Sookon\Helpers\Config;
use Sookon\File\Files as files; use Sookon\File\Files as files;

View File

@ -1,354 +1,354 @@
<?php <?php
namespace Sookon\File; namespace Sookon\File;
use Zend\EventManager\EventManagerInterface; use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManager; use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface; use Zend\EventManager\EventManagerAwareInterface;
use Sookon\File\Thumbnail; use Sookon\File\Thumbnail;
use Sookon\Helpers\Pdo; use Sookon\Helpers\PDO;
use Sookon\Helpers\Config; use Sookon\Helpers\Config;
use Sookon\File\Listener\FileListener as Listener; use Sookon\File\Listener\FileListener as Listener;
class Files implements EventManagerAwareInterface class Files implements EventManagerAwareInterface
{ {
public $tbl_att = "tbl_attachments"; public $tbl_att = "tbl_attachments";
public $db; public $db;
public $config; public $config;
public $source = ""; public $source = "";
protected $events = NULL; //事件 protected $events = NULL; //事件
function __construct() function __construct()
{ {
$this->db = new Pdo(); $this->db = new Pdo();
$this->config = Config::get(); $this->config = Config::get();
$Listener = new Listener(); $Listener = new Listener();
$this->getEventManager()->attachAggregate($Listener); $this->getEventManager()->attachAggregate($Listener);
} }
public function setEventManager(EventManagerInterface $events) public function setEventManager(EventManagerInterface $events)
{ {
$events->setIdentifiers(array( $events->setIdentifiers(array(
__CLASS__, __CLASS__,
get_called_class(), get_called_class(),
)); ));
$this->events = $events; $this->events = $events;
return $this; return $this;
} }
public function getEventManager() public function getEventManager()
{ {
if (NULL === $this->events) { if (NULL === $this->events) {
$this->setEventManager(new EventManager()); $this->setEventManager(new EventManager());
} }
return $this->events; return $this->events;
} }
/** /**
* upload * upload
* *
* 文件上传 * 文件上传
* *
* @param Array $files e.g. $_FILES['Filedata'] * @param Array $files e.g. $_FILES['Filedata']
* *
* return Array $msg e.g. if($msg['error']) * return Array $msg e.g. if($msg['error'])
*/ */
public function upload($files) public function upload($files)
{ {
if (empty($files) !== false) { if (empty($files) !== false) {
return array("error"=>"请选择要上传的文件."); return array("error"=>"请选择要上传的文件.");
} }
if (is_uploaded_file($files['tmp_name']) === false) { if (is_uploaded_file($files['tmp_name']) === false) {
return array("error"=>"文件上传失败,请重新上传"); return array("error"=>"文件上传失败,请重新上传");
} }
$conf = Config::get('file'); $conf = Config::get('file');
$source = $this->source; $source = $this->source;
$file = $files; $file = $files;
$results = $this->getEventManager()->trigger('upload.checkExt', $this, compact('source','file','conf')); $results = $this->getEventManager()->trigger('upload.checkExt', $this, compact('source','file','conf'));
$cache_data = $results->last(); $cache_data = $results->last();
if($cache_data !== true) if($cache_data !== true)
{ {
return $cache_data; return $cache_data;
} }
$results = $this->getEventManager()->trigger('upload.checkSize', $this, compact('file','conf')); $results = $this->getEventManager()->trigger('upload.checkSize', $this, compact('file','conf'));
$cache_data = $results->last(); $cache_data = $results->last();
if($cache_data !== true) if($cache_data !== true)
{ {
return $cache_data; return $cache_data;
} }
$msg = array(); $msg = array();
$file_name = $files['name']; //原文件名 $file_name = $files['name']; //原文件名
$file_size = $files['size']; //文件大小 $file_size = $files['size']; //文件大小
$results = $this->makeUploadTarget($conf); $results = $this->makeUploadTarget($conf);
if(isset($results['error'])) if(isset($results['error']))
{ {
return $results; return $results;
}//文件夹问题 }//文件夹问题
$new_file_basename = $this->gen_uuid(); $new_file_basename = $this->gen_uuid();
$file_ext = $this->getFileTextExt($file_name); $file_ext = $this->getFileTextExt($file_name);
$new_file_name = $new_file_basename . '.' . $file_ext;//新文件名 $new_file_name = $new_file_basename . '.' . $file_ext;//新文件名
//移动文件 //移动文件
$file_path = $results['save_path'] . $new_file_name ; $file_path = $results['save_path'] . $new_file_name ;
if (move_uploaded_file($file['tmp_name'], $file_path) === false) { if (move_uploaded_file($file['tmp_name'], $file_path) === false) {
return array("error"=>"上传失败,请重试"); return array("error"=>"上传失败,请重试");
} }
$dbsave = $db_path = $results['dbsave']; $dbsave = $db_path = $results['dbsave'];
$dbsave .= $new_file_name;//数据库最终存储的文件 $dbsave .= $new_file_name;//数据库最终存储的文件
$file_url = $dbsave;//文件链接 $file_url = $dbsave;//文件链接
$results = $this->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename')); $results = $this->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename'));
$thumbnail = $results->last(); $thumbnail = $results->last();
$msg['file_url'] = $file_url; $msg['file_url'] = $file_url;
$msg['file_size'] = $file_size; $msg['file_size'] = $file_size;
$msg['db_path'] = $conf->upload . $dbsave; $msg['db_path'] = $conf->upload . $dbsave;
$msg['realname'] = $file_name; $msg['realname'] = $file_name;
$msg['file_ext'] = $file_ext; $msg['file_ext'] = $file_ext;
$msg['file_type'] = $this->getFileMime($file_path); $msg['file_type'] = $this->getFileMime($file_path);
$msg['thumb'] = $thumbnail; $msg['thumb'] = $thumbnail;
return $msg; return $msg;
}//文件上传 }//文件上传
//生成上传文件的地址 //生成上传文件的地址
public function makeUploadTarget($conf) public function makeUploadTarget($conf)
{ {
//文件保存目录路径 //文件保存目录路径
$save_path = $conf->upload; $save_path = $conf->upload;
if (is_dir($save_path) === false) { if (is_dir($save_path) === false) {
return array("error"=>"上传目录不存在。请联系管理员"); return array("error"=>"上传目录不存在。请联系管理员");
} }
if (is_writable($save_path) === false) { if (is_writable($save_path) === false) {
return array("error"=>"上传目录没有写权限。请联系管理员"); return array("error"=>"上传目录没有写权限。请联系管理员");
} }
$dbsave = ""; //数据库中存放的路径 $dbsave = ""; //数据库中存放的路径
$y = date("Y"); $y = date("Y");
$m = date("m"); $m = date("m");
$d = date("d"); $d = date("d");
$save_path .= $y . "/"; $save_path .= $y . "/";
$dbsave .= $y.'/'; $dbsave .= $y.'/';
if (!file_exists($save_path)) { if (!file_exists($save_path)) {
mkdir($save_path); mkdir($save_path);
} }
$save_path .= $m . "/"; $save_path .= $m . "/";
$dbsave .= $m.'/'; $dbsave .= $m.'/';
if (!file_exists($save_path)) { if (!file_exists($save_path)) {
mkdir($save_path); mkdir($save_path);
} }
$save_path .= $d . "/"; $save_path .= $d . "/";
$dbsave .= $d.'/'; $dbsave .= $d.'/';
if (!file_exists($save_path)) { if (!file_exists($save_path)) {
mkdir($save_path); mkdir($save_path);
} }
return array("save_path"=>$save_path,"dbsave"=>$dbsave); return array("save_path"=>$save_path,"dbsave"=>$dbsave);
}//创建上传文件的地址 }//创建上传文件的地址
//获取文件扩展名 //获取文件扩展名
public function getFileTextExt($file_name) public function getFileTextExt($file_name)
{ {
$temp_arr = explode(".", $file_name); $temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr); $file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext); $file_ext = trim($file_ext);
$file_ext = strtolower($file_ext); $file_ext = strtolower($file_ext);
return $file_ext; return $file_ext;
} }
//获取文件Mime通过finfo扩展 //获取文件Mime通过finfo扩展
public function getFileMime($file_name) public function getFileMime($file_name)
{ {
$finfo = finfo_open(FILEINFO_MIME_TYPE); $finfo = finfo_open(FILEINFO_MIME_TYPE);
$filetype = finfo_file($finfo, $file_name) ; //文件mime类型 $filetype = finfo_file($finfo, $file_name) ; //文件mime类型
finfo_close($finfo); finfo_close($finfo);
return $filetype; return $filetype;
} }
//文件名uuid //文件名uuid
public function gen_uuid() { public function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low" // 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid" // 16 bits for "time_mid"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version", // 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4 // four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000, mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res", // 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low", // 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1 // two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000, mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node" // 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
); );
} }
/** /**
* getOne() * getOne()
* *
* 从数据库获取单个文件的信息 * 从数据库获取单个文件的信息
* *
* @param int id * @param int id
* *
* return array * return array
*/ */
public function getOne($id){ public function getOne($id){
$db = $this->db; $db = $this->db;
//下载单个附件 //下载单个附件
$sql = $db->query("select a.* from ".$this->tbl_att." a where a.id=$id order by a.ts_created desc"); $sql = $db->query("select a.* from ".$this->tbl_att." a where a.id=$id order by a.ts_created desc");
$att = $sql->fetch(); $att = $sql->fetch();
if(empty($att['id'])) if(empty($att['id']))
{ {
return false; return false;
} }
return $att; return $att;
} }
public function delete($id){ public function delete($id){
$att = $this->getOne($id); $att = $this->getOne($id);
if(!empty($att['thumb'])) if(!empty($att['thumb']))
{ {
$thumbs = json_decode($att['thumb'],true); $thumbs = json_decode($att['thumb'],true);
if(count($thumbs)>0) if(count($thumbs)>0)
{ {
foreach($thumbs as $v) foreach($thumbs as $v)
{ {
@unlink($v['file']); @unlink($v['file']);
} }
} }
} }
if(file_exists($att['filename'])){ if(file_exists($att['filename'])){
if(unlink($att['filename'])) if(unlink($att['filename']))
{ {
$sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id"; $sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id";
return $this->db->exec($sql); return $this->db->exec($sql);
}else{ }else{
return "删除失败"; return "删除失败";
} }
}else{ }else{
$sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id"; $sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id";
if($this->db->exec($sql)) if($this->db->exec($sql))
{ {
return "文件不存在,数据库记录删除成功"; return "文件不存在,数据库记录删除成功";
}else{ }else{
return "文件不存在,数据库记录删除失败"; return "文件不存在,数据库记录删除失败";
} }
} }
} }
public function getAll($fields = ""){ public function getAll($fields = ""){
$wheresql = array(); $wheresql = array();
if($fields!="") if($fields!="")
{ {
$wheresql[] = " a.id IN ($fields) "; $wheresql[] = " a.id IN ($fields) ";
} }
if(count($wheresql)>0) if(count($wheresql)>0)
{ {
$wheresql = " WHERE ".join(",",$wheresql); $wheresql = " WHERE ".join(",",$wheresql);
}else{ }else{
$wheresql = ""; $wheresql = "";
} }
$sql = "SELECT a.*,u.username,u.id as uid FROM ".$this->tbl_att." a $sql = "SELECT a.*,u.username,u.id as uid FROM ".$this->tbl_att." a
LEFT JOIN tbl_member u ON a.userid=u.id LEFT JOIN tbl_member u ON a.userid=u.id
$wheresql $wheresql
ORDER BY a.id DESC"; ORDER BY a.id DESC";
$rs = $this->db->query($sql); $rs = $this->db->query($sql);
$rows = $rs->fetchAll(); $rows = $rs->fetchAll();
return $rows; return $rows;
} }
/** /**
* downloadFile * downloadFile
* *
* 文件下载 * 文件下载
* *
* @param String $fullPath e.g. Zend_Registry::get('upload').$fileurl * @param String $fullPath e.g. Zend_Registry::get('upload').$fileurl
* *
* return file * return file
*/ */
static function downloadFile( $fullPath ){ static function downloadFile( $fullPath ){
//判断是否已经输出头部 //判断是否已经输出头部
if( headers_sent() ) if( headers_sent() )
{ {
return false; return false;
} }
else{ else{
// Required for some browsers // Required for some browsers
@apache_setenv('no-gzip', 1); @apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0); @ini_set('zlib.output_compression', 0);
// File Exists? // File Exists?
if( file_exists($fullPath) ){ if( file_exists($fullPath) ){
// Parse Info / Get Extension // Parse Info / Get Extension
$fsize = filesize($fullPath); $fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath); $path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]); $ext = strtolower($path_parts["extension"]);
// Determine Content Type // Determine Content Type
switch ($ext) { switch ($ext) {
case "pdf": $ctype="application/pdf"; break; case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break; case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break; case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break; case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break; case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break; case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break; case "png": $ctype="image/png"; break;
case "jpeg": case "jpeg":
case "jpg": $ctype="image/jpg"; break; case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download"; default: $ctype="application/force-download";
} }
header('Content-Description: File Transfer'); header('Content-Description: File Transfer');
header("Cache-Control: private",false); header("Cache-Control: private",false);
header("Content-Type: $ctype"); header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" ); header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary\n"); header("Content-Transfer-Encoding: binary\n");
header("Expires: 0"); header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public"); header("Pragma: public");
header("Content-Length: ".$fsize); header("Content-Length: ".$fsize);
ob_clean(); ob_clean();
flush(); flush();
if(readfile($fullPath)) if(readfile($fullPath))
return true; return true;
else else
return false; return false;
} else } else
{return false;} {return false;}
} }
} //文件下载 } //文件下载
} }