以下是我完整的控制器,大家可以参考一下,因为只是简单地实现功能,所以很多东西没有写得严谨,大家可以根据思路写自己的功能。
<?php // 后台用户模块 class FileAction extends CommonAction { protected $tableName = 'userfiles'; public function index(){ $M = M($this->tableName); $typeArr = array('1'=>'图片', '2'=>'flash'); $map = array(); $count = $M->where($map)->count(); import('ORG.util.Page');// 导入分页类 $Page = new Page($count,20); $show = $Page->show(); $list = $M->where($map)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select(); foreach($list as $key=>$val){ $list[$key]['exist'] = file_exists('.'.$val['path']) ? '正常' : '<span class=\'red\'>文件丢失</span>'; $list[$key]['typename'] = $typeArr[$val['type']]; } $this->assign('list',$list); $this->assign('page',$show); $this->assign('typeList',$typeList); $this->display(); } public function add(){ $M = M($this->tableName); if(IS_POST){ //$data = $_POST['data']; //将表单传过来的data值赋予data数组 $id = I('id'); $hiddenFilePath = I('hiddenFilePath'); //隐藏文件路径 更新时用于删除文件用 $data['filename'] = I('name'); $data['type'] = I('type'); $data['remark'] = I('remark'); //获取上传文件信息 $fileInfo = $this->getFileInfo($data['type']); if( empty($id) && empty($fileInfo) ){ $this->ajaxReturn(array('status'=>1, 'info'=>'操作成功')); }elseif( !empty($fileInfo) ){ //如果文件不为空,则将文件信息赋予DATA数组 $data['savepath'] = $fileInfo['savepath']; $data['extension'] = $fileInfo['extension']; $data['size'] = $fileInfo['size']; $data['savename'] = $fileInfo['savename']; } if( empty($data['filename']) ){ $data['filename'] = $fileInfo['name']; } $data['user_id'] = $this->userId; $data['create_time'] = NOW_TIME; if(!$id){ //添加 $M->add($data); $result = array('status'=>1, 'info'=>'操作成功'); }else{ if($data['savepath']){ $thumb = getImgThumb($hiddenFilePath); if( file_exists('.'.$hiddenFilePath) ) unlink ('.'.$hiddenFilePath); //删除已经存在的文件,重新上传新文件 if( file_exists('.'.$thumb) ) unlink ('.'.$thumb); } $M->where('id='.$id)->save($data); $result = array('status'=>1, 'info'=>'操作成功'); } $this->ajaxReturn($result); }else{ $id = I('id'); if( !empty($id) ){ $info = $M->where('id='.$id)->find(); $this->assign('info',$info); } $this->display(); } } public function del(){ $M = M("File"); $id = $_REQuEST['id']; $map['id'] = array('eq',$id); $savepath = $M->where($map)->getField('savepath'); if(file_exists(WEB_ROOT.$savepath)) unlink(WEB_ROOT.$savepath); $delNum = $M->where($map)->delete(); if(!empty($delNum)){ $this->ajaxReturn(1,'成功删除'.$delNum.'条数据!',1); }else{ $this->ajaxReturn(0,'删除失败!',0); } } public function updateSort(){ $idArr = $_POST['idArr']; $sortArr = $_POST['sortArr']; } public function getFileInfo($type=1){ if( empty($_FILES) ) return false; //$uploadFileName = date('YmdHis').rand(1000,9999); if($type==1){ $extendPath = 'images/'.date(Ymd).'/'; }else{ $extendPath = 'flash/'.date(Ymd).'/'; } import('ORG.Net.uploadFile'); $upload = new uploadFile();// 实例化上传类 //$upload->saveRule = $uploadFileName; $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg','doc','wps','xls','csv','rar','tar','txt');// 设置附件上传类型 $upload->savePath = './uploads/'.$extendPath; $upload->thumb = true; $upload->thumbMaxWidth = '50,200'; $upload->thumbMaxHeight = '50,200'; $upload->thumbPrefix = 'small_'; if(!$upload->upload()) {// 上传错误提示错误信息 $this->ajaxReturn(0,$upload->getErrorMsg(),0); }else{ $fileInfo = $upload->getuploadFileInfo(); $fileInfo[0]['savepath'] = '/uploads/'.$extendPath.$fileInfo[0]['savename']; } return $fileInfo[0]; } public function downloadFile(){ $M = M($this->tableName); $map['id'] = I('fileId'); $info = $M->where($map)->find(); $filepath = '.'.$info['path']; if( !file_exists($filepath) ){ echo '文件不存在!'; exit; } //$M->where($map)->setInc('download'); $file = fopen($filepath,"r"); // 打开文件 // 输入文件标签 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length: ".filesize($filepath)); Header("Content-Disposition: attachment; filename=" . $info['savename']); // 输出文件内容 echo fread($file,filesize($filepath)); fclose($file); exit; } public function getDownloadNum(){ $M = M($this->tableName); $where['id'] = (int)$_REQuEST['id']; $downloadNum = $M->where($where)->getField('download'); $this->ajaxReturn($downloadNum,'获取成功!',1); } }