看下项目要求:可以选择是否存入数据库,指定文件类型,文件大小,缩略图及缩略图大小
-
class AttachModel extends Model{
-
- /**
- * 附件上传
- * @param string $type 文件类型:jpg,png
- * @param int $maxsize 最大上传容量:默认100Kb
- * @param string $model 上传所在模块
- * @param bool $insert 是否写入数据库
- * @param bool $thumb 是否生成缩略图
- * @param string $wh 缩略图的宽高
- * Example: $upload->upload(null,102400,APP_NAME,true,true,array('300','225'));
- */
- public function upload($type = null, $maxsize = '102400', $model = null, $insert = true, $thumb = false, $wh = array('160', '120')){
- //导入上传类
- import('ORG.NET.UploadFile');
- $upload = new UploadFile();
- $upload->maxSize = $maxsize;
- if ($type){
- $type = explode(',', $type);
- $upload->allowExts = $type;
- }else{
- $upload->allowExts = array('jpg','png','gif','jpeg');
- }
- if ($model){
- $upload->savePath = '../Public/Uploads/'.$model.'/';
- }else{
- $upload->savePath = '../Public/Uploads/';
- }
- if ($thumb){
- $upload->thumb = true;
- $upload->thumbPrefix = 'zj_';
- $upload->thumbMaxWidth = $wh[0];
- $upload->thumbMaxHeight = $wh[1];
- }
- $upload->saveRule = uniqid;//上传图片命名规则
- if (!$upload->upload()) {
- return $upload->getErrorMsg();
- }else{
- $uploadlist = $upload->getUploadFileInfo();
- }
- if ($insert){
- return $this->_insert($uploadlist);
- }else{
- return $uploadlist;
- }
- }
- /*
- * 上传的附件整合成attach所需数据,存入表并返回数组
- * */
- private function _insert($uploadlist){
- $j = count($uploadlist);
- $v = array();
- foreach ($uploadlist as $key => $value)
- {
- $v[$key]['name'] = $value['name'];
- $v[$key]['hashname'] = $value['savename'];
- $v[$key]['savepath'] = substr($value['savepath'], 2);
- $v[$key]['bsize'] = $value['size'];
- $v[$key]['user_id'] = $_SESSION[C('USER_AUTH_KEY')];
- $v[$key]['create_time'] = time();
- $v[$key]['model_name'] = APP_NAME;
- $this->add($v[$key]);
- if($this->thumb)
- {
- $v[$key]['prefix'] = $this->thumbPrefix;
- }
- $v[$key]['id'] = M('Attach')->getLastInsID();
- }
- return $v;
- }
-
- }
复制代码
- DROP TABLE IF EXISTS `zj_attach`;
- CREATE TABLE `zj_attach` (
- `id` int(10) NOT NULL auto_increment,
- `name` varchar(100) NOT NULL COMMENT '附件名称',
- `hashname` varchar(100) default NULL,
- `status` tinyint(1) default '1' COMMENT '附件状态{1:启用,0:禁用}',
- `savepath` varchar(100) default NULL COMMENT '存储地址',
- `bsize` varchar(100) default NULL COMMENT '附件大小',
- `model_name` varchar(50) default NULL COMMENT '所属模块',
- `user_id` int(10) default NULL COMMENT '上传用户id',
- `create_time` int(10) default NULL COMMENT '上传时间',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
复制代码
|