. * * @copyright Copyright 2009-2010 青岛易软天创网络科技有限公司(www.cnezsoft.com) * @author Chunsheng Wang * @package file * @version $Id$ * @link http://www.zentaoms.com */ ?> now = time(); $this->setSavePath(); $this->setWebPath(); } /* 通过对象类型获取文件列表。*/ public function getByObject($objectType, $objectID) { return $this->dao->select('*')->from(TABLE_FILE)->where('objectType')->eq($objectType)->andWhere('objectID')->eq((int)$objectID)->orderBy('id')->fetchAll(); } /* 获得某一个文件的信息。*/ public function getById($fileID) { $file = $this->dao->findById($fileID)->from(TABLE_FILE)->fetch(); $file->webPath = $this->webPath . $file->pathname; $file->realPath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . $file->pathname; return $file; } /* 保存上传的文件。*/ public function saveUpload($objectType = '', $objectID = '', $extra = '') { $fileTitles = array(); $now = helper::today(); $files = $this->getUpload(); foreach($files as $id => $file) { move_uploaded_file($file['tmpname'], $this->savePath . $file['pathname']); $file['objectType'] = $objectType; $file['objectID'] = $objectID; $file['addedBy'] = $this->app->user->account; $file['addedDate'] = $now; $file['extra'] = $extra; unset($file['tmpname']); $this->dao->insert(TABLE_FILE)->data($file)->exec(); $fileTitles[$this->dao->lastInsertId()] = $file['title']; } return $fileTitles; } /* 获得文件数量。*/ public function getCount() { return count($this->getUpload()); } /* 获取上传的文件信息。*/ private function getUpload($htmlTagName = 'files') { $files = array(); if(!isset($_FILES[$htmlTagName])) return $files; /* 表单定义中的变量名是数组。*/ if(is_array($_FILES[$htmlTagName]['name'])) { extract($_FILES[$htmlTagName]); foreach($name as $id => $filename) { if(empty($filename)) continue; $file['extension'] = $this->getExtension($filename); $file['pathname'] = $this->setPathName($id, $file['extension']); $file['title'] = !empty($_POST['labels'][$id]) ? htmlspecialchars($_POST['labels'][$id]) : str_replace('.' . $file['extension'], '', $filename); $file['size'] = $size[$id]; $file['tmpname'] = $tmp_name[$id]; $files[] = $file; } } else { if(empty($_FILES[$htmlTagName]['name'])) return $files; extract($_FILES[$htmlTagName]); $file['extension'] = $this->getExtension($name); $file['pathname'] = $this->setPathName(0, $file['extension']); $file['title'] = !empty($_POST['labels'][0]) ? htmlspecialchars($_POST['labels'][0]) : pathinfo($filename, PATHINFO_FILENAME); $file['size'] = $size; $file['tmpname'] = $tmp_name; return array($file); } return $files; } /* 获取文件扩展名。*/ private function getExtension($filename) { $extension = pathinfo($filename, PATHINFO_EXTENSION); if(empty($extension)) return 'txt'; if(strpos($this->config->file->dangers, $extension) !== false) return 'txt'; return $extension; } /* 设置要存储的文件名。*/ private function setPathName($fileID, $extension) { $sessionID = session_id(); $randString = substr($sessionID, mt_rand(0, strlen($sessionID) - 5), 3); return date('Ym/dHis', $this->now) . $fileID . mt_rand(0, 10000) . $randString . '.' . $extension; } /* 设置存储路径。*/ private function setSavePath() { $savePath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . date('Ym/', $this->now); if(!file_exists($savePath)) mkdir($savePath, 0777, true); $this->savePath = dirname($savePath) . '/'; } /* 设置web访问路径。*/ private function setWebPath() { $this->webPath = $this->app->getWebRoot() . "data/upload/{$this->app->company->id}/"; } }