public function actionUpload() { $uptype = zmf::filterInput($_GET['type'], 't', 1); $logid = zmf::filterInput($_GET['id']); //所属对象 $reImgsize = zmf::filterInput($_GET['imgsize']); //返回图片的尺寸 $fileholder = zmf::filterInput($_GET['fileholder'], 't', 1); //上传控件的ID if (!isset($uptype) or !in_array($uptype, array('posts', 'siteinfo'))) { $this->jsonOutPut(0, '请设置上传所属类型' . $uptype); } if (Yii::app()->request->getParam('PHPSESSID')) { Yii::app()->session->close(); $res = Yii::app()->session->setSessionID(Yii::app()->request->getParam('PHPSESSID')); Yii::app()->session->open(); } if (Yii::app()->user->isGuest) { $this->jsonOutPut(0, '请先登录'); } if (!$fileholder) { $fileholder = 'filedata'; } if (!isset($_FILES[$fileholder]) || !is_uploaded_file($_FILES[$fileholder]["tmp_name"]) || $_FILES[$fileholder]["error"] != 0) { $this->jsonOutPut(0, '无效上传,请重试'); } $img = CUploadedFile::getInstanceByName($fileholder); $ext = $img->getExtensionName(); $size = $img->getSize(); if ($size > zmf::config('imgMaxSize')) { $this->jsonOutPut(0, '上传文件最大尺寸为:' . tools::formatBytes(zmf::config('imgMaxSize'))); } $upExt = zmf::config("imgAllowTypes"); if (!preg_match('/^(' . str_replace('*.', '|', str_replace(';', '', $upExt)) . ')$/i', $ext)) { $this->jsonOutPut(0, '上传文件扩展名必需为:' . $upExt); } $sizeinfo = getimagesize($_FILES[$fileholder]["tmp_name"]); if ($sizeinfo['0'] < zmf::config('imgMinWidth') or $sizeinfo[1] < zmf::config('imgMinHeight')) { $this->jsonOutPut(0, "要求上传的图片尺寸,宽不能不小于" . zmf::config('imgMinWidth') . "px,高不能小于" . zmf::config('imgMinHeight') . "px."); } $ctime = zmf::now(); $dir = zmf::uploadDirs($ctime, 'app', $uptype); zmf::createUploadDir($dir); $fileName = zmf::uuid() . '.' . $ext; $origin = $dir; if (move_uploaded_file($_FILES[$fileholder]["tmp_name"], $origin . $fileName)) { $data = array(); $status = Posts::STATUS_NOTPASSED; $data['uid'] = zmf::uid(); $data['logid'] = $logid; $data['filePath'] = $fileName; $data['fileDesc'] = ''; $data['classify'] = $uptype; $data['covered'] = '0'; $data['cTime'] = $ctime; $data['status'] = $status; $data['width'] = $sizeinfo[0]; $data['height'] = $sizeinfo[1]; $data['size'] = $size; $model = new Attachments(); $model->attributes = $data; if ($model->save()) { $attachid = $model->id; $returnImgDir = zmf::getUpExtraUrl($ctime); $saveName = $uptype . '/' . $returnImgDir . '/' . $fileName; $accessKey = zmf::config('qiniuAk'); $secretKey = zmf::config('qiniuSk'); $bucket = zmf::config('qiniuBucket'); if ($accessKey && $secretKey && $bucket) { $auth = new Auth($accessKey, $secretKey); $token = $auth->uploadToken($bucket); $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->putFile($token, $saveName, $origin . $fileName); if ($err !== null) { zmf::fp(var_export($err)); $this->jsonOutPut(0, '上传至云服务错误'); } } $returnimg = zmf::uploadDirs($ctime, 'site', $uptype) . $fileName; $returnimg = zmf::getThumbnailUrl($returnimg, '650', $uptype); $_attr = array('id' => $attachid, 'imgurl' => $returnimg); $html = ''; if ($uptype == 'posts') { //$html= $this->renderPartial('/posts/_addImg',array('data'=>$_attr),true); } $outPutData = array('status' => 1, 'attachid' => $attachid, 'imgsrc' => $returnimg, 'html' => $html); $json = CJSON::encode($outPutData); echo $json; } else { $this->jsonOutPut(0, '写入数据库错误'); } } }
/** * 不入数据库的上传 * 不做压缩和缩略图处理 */ public function actionSimpleUpload() { $uptype = zmf::filterInput($_GET['type'], 't', 1); $fileholder = zmf::filterInput($_GET['fileholder'], 't', 1); //上传控件的ID $fileName = zmf::filterInput($_GET['fileName'], 't', 1); //上传后保存名字 $keyid = zmf::filterInput($_GET['keyid']); //所属对象ID if (!isset($uptype) or !in_array($uptype, array('avatar'))) { $this->jsonOutPut(0, '请设置上传所属类型' . $uptype); } if (Yii::app()->request->getParam('PHPSESSID')) { Yii::app()->session->close(); Yii::app()->session->setSessionID(Yii::app()->request->getParam('PHPSESSID')); Yii::app()->session->open(); } if (Yii::app()->user->isGuest) { $this->jsonOutPut(0, Yii::t('default', 'loginfirst')); } // $checkInfo = UserPower::check('addImage', true); // if (!$checkInfo['status']) { // $this->jsonOutPut(0, $checkInfo['msg']); // } if (!$fileholder) { $fileholder = 'filedata'; } if (!isset($_FILES[$fileholder]) || !is_uploaded_file($_FILES[$fileholder]["tmp_name"]) || $_FILES[$fileholder]["error"] != 0) { $this->jsonOutPut(0, '无效上传,请重试'); } $img = CUploadedFile::getInstanceByName($fileholder); $ext = $img->getExtensionName(); $size = $img->getSize(); if ($size > zmf::config('imgMaxSize')) { $this->jsonOutPut(0, '上传文件最大尺寸为:' . tools::formatBytes(zmf::config('imgMaxSize'))); } $upExt = zmf::config("imgAllowTypes"); if (!preg_match('/^(' . str_replace('*.', '|', str_replace(';', '', $upExt)) . ')$/i', $ext)) { $this->jsonOutPut(0, '上传文件扩展名必需为:' . $upExt); } if (!$fileName) { $fileName = uniqid() . '.' . $ext; } if ($uptype == 'avatar') { $fileName = $keyid . '.jpg'; } $ctime = zmf::now(); $dirs = zmf::uploadDirs($ctime, 'app', $uptype, null, true); $fileName = uniqid() . '.' . $ext; $origin = $dirs['origin']; unset($dirs['origin']); $fileNameWithoutDomain = zmf::getUpExtraUrl($ctime) . '/600_' . $fileName; //相对于网站的存放路径 if (move_uploaded_file($_FILES[$fileholder]["tmp_name"], $origin . $fileName)) { Yii::import('application.vendors.thinkphp.*'); require_once 'ImageGd.php'; foreach ($dirs as $dk => $_dir) { $image = new ImageGd($origin . $fileName); $image->thumb($dk, $dk, 'center'); $image->save($_dir . $fileName); } Users::model()->updateByPk($this->uid, array('avatar' => $fileNameWithoutDomain)); $imgsize = $reImgsize > 0 ? $reImgsize : 170; $returnimg = zmf::uploadDirs($ctime, 'site', $uptype, $imgsize) . $fileName; $outPutData = array('status' => 1, 'imgsrc' => $returnimg, 'attachid' => ''); $json = CJSON::encode($outPutData); echo $json; } else { $this->jsonOutPut(0, '上传失败'); } }