db = \Zend_Registry::get('db'); }else{ $this->db = $db; } $this->config = \Zend_Registry::get('config'); $this->event = new \Zend_EventManager_EventManager(); $FileListener = new FileListener(); @$this->events()->attachAggregate($FileListener); } 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"=>"文件上传失败,请重新上传"); } if($file['size'] > 20 * 1024 * 1024) { 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 = "",$append = false,$filename="",$makeThumb = false) { if (empty($files) !== false) { return array("error"=>"请选择要上传的文件"); } if (@is_uploaded_file($files['tmp_name']) === false) { return array("error"=>"文件上传失败,请重新上传"); } $file = $files; $results = $this->events()->trigger('upload.checkExt', $this, compact('file')); $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($dir,$append); 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;//文件链接 $msg['file_url'] = $file_url; $msg['file_size'] = $file_size; $msg['db_path'] = $dbsave; $msg['realname'] = $file_name; $msg['file_ext'] = $file_ext; $msg['file_mime'] = $this->getFileMime($file_path); return $msg; }//文件上传 //生成上传文件的地址 public function makeUploadTarget($dir = "",$append = false) { //文件保存目录路径 if(empty($dir)) { $save_path = $this->config->upload; }else{ if($append === true) { $save_path = $this->config->upload.$dir; }else{ $save_path = $dir; } } if (@is_dir($save_path) === false) { return array("error"=>"上传目录不存在。请联系管理员"); } if (@is_writable($save_path) === false) { return array("error"=>"上传目录没有写权限。请联系管理员"); } $dbsave = ""; //数据库中存放的路径 if($append === true) { $dbsave = $dir; } $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(file_exists($this->config->upload.$att['filename'])){ if(unlink($this->config->upload.$att['filename'])) { $sql = "DELETE FROM ".$this->tbl_att." WHERE id=$id"; if($this->db->exec($sql)) { return true; }else{ return "文件删除成功,数据库记录删除失败"; } }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;} } } //文件下载 }