From 90595c2e6a2d8530d686505a70618e83af9afe0c Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Wed, 19 Mar 2014 04:05:20 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9Helpers\PDO=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/ServiceController.php | 2 +- vendor/Sookon/File/Files.php | 706 +++++++++--------- 2 files changed, 354 insertions(+), 354 deletions(-) diff --git a/module/Application/src/Application/Controller/ServiceController.php b/module/Application/src/Application/Controller/ServiceController.php index 5955f558..2091c8a0 100644 --- a/module/Application/src/Application/Controller/ServiceController.php +++ b/module/Application/src/Application/Controller/ServiceController.php @@ -15,7 +15,7 @@ use Zend\View\Model\JsonModel; use Zend\Http\PhpEnvironment\Request; use Sookon\Helpers\View as view; use Sookon\Helpers\Dbh as dbh; -use Sookon\Helpers\Pdo; +use Sookon\Helpers\PDO; use Sookon\Helpers\Config; use Sookon\File\Files as files; diff --git a/vendor/Sookon/File/Files.php b/vendor/Sookon/File/Files.php index b599c913..45e96463 100644 --- a/vendor/Sookon/File/Files.php +++ b/vendor/Sookon/File/Files.php @@ -1,354 +1,354 @@ -db = new Pdo(); - $this->config = Config::get(); - - $Listener = new Listener(); - $this->getEventManager()->attachAggregate($Listener); - } - - public function setEventManager(EventManagerInterface $events) - { - $events->setIdentifiers(array( - __CLASS__, - get_called_class(), - )); - $this->events = $events; - return $this; - } - - public function getEventManager() - { - if (NULL === $this->events) { - $this->setEventManager(new EventManager()); - } - return $this->events; - } - - /** - * upload - * - * 文件上传 - * - * @param Array $files e.g. $_FILES['Filedata'] - * - * return Array $msg e.g. if($msg['error']) - */ - public function upload($files) - { - if (empty($files) !== false) { - return array("error"=>"请选择要上传的文件."); - } - - if (is_uploaded_file($files['tmp_name']) === false) { - return array("error"=>"文件上传失败,请重新上传"); - } - - $conf = Config::get('file'); - - $source = $this->source; - $file = $files; - - $results = $this->getEventManager()->trigger('upload.checkExt', $this, compact('source','file','conf')); - $cache_data = $results->last(); - - if($cache_data !== true) - { - return $cache_data; - } - - $results = $this->getEventManager()->trigger('upload.checkSize', $this, compact('file','conf')); - $cache_data = $results->last(); - - 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->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename')); - $thumbnail = $results->last(); - - $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;} - } - } //文件下载 +db = new Pdo(); + $this->config = Config::get(); + + $Listener = new Listener(); + $this->getEventManager()->attachAggregate($Listener); + } + + public function setEventManager(EventManagerInterface $events) + { + $events->setIdentifiers(array( + __CLASS__, + get_called_class(), + )); + $this->events = $events; + return $this; + } + + public function getEventManager() + { + if (NULL === $this->events) { + $this->setEventManager(new EventManager()); + } + return $this->events; + } + + /** + * upload + * + * 文件上传 + * + * @param Array $files e.g. $_FILES['Filedata'] + * + * return Array $msg e.g. if($msg['error']) + */ + public function upload($files) + { + if (empty($files) !== false) { + return array("error"=>"请选择要上传的文件."); + } + + if (is_uploaded_file($files['tmp_name']) === false) { + return array("error"=>"文件上传失败,请重新上传"); + } + + $conf = Config::get('file'); + + $source = $this->source; + $file = $files; + + $results = $this->getEventManager()->trigger('upload.checkExt', $this, compact('source','file','conf')); + $cache_data = $results->last(); + + if($cache_data !== true) + { + return $cache_data; + } + + $results = $this->getEventManager()->trigger('upload.checkSize', $this, compact('file','conf')); + $cache_data = $results->last(); + + 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->getEventManager()->trigger('upload.makeThumb', $this, compact('conf','file_path','db_path','file_ext','new_file_basename')); + $thumbnail = $results->last(); + + $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;} + } + } //文件下载 } \ No newline at end of file