- <?php
- 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;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/270620134342.html
来源: http://www.codesnippet.cn/detail/270620134342.html