예제 #1
0
 /**
  *
  * 文件上传处理
  * @param Request $request
  * @return bool|string
  * @throws \Exception
  */
 public function upload(Request $request)
 {
     if (!$request instanceof Request) {
         throw new \Exception(BaseConst::UPLOAD_PARAMETER_ERROR_MSG, BaseConst::UPLOAD_PARAMETER_ERROR);
     }
     if (!$this->targetAbs) {
         $this->setTargetDir();
     }
     $files = $request->getFiles()->toArray();
     if (empty($files)) {
         throw new \Exception(BaseConst::UPLOAD_PARAMETER_ERROR_MSG, BaseConst::UPLOAD_PARAMETER_ERROR);
     }
     $transer = new \Zend\File\Transfer\Adapter\Http();
     $filters = array();
     $result = array();
     // 传输文件准备
     foreach ($files as $k => $v) {
         $tmp[] = $v['name'];
         $ext = strstr($v['name'], '.');
         $tmpName = strstr($v['name'], '.', true);
         $filters[] = new \Zend\Filter\File\Rename(array('target' => $this->targetAbs . md5($tmpName) . $ext, "randomize" => true));
         // 判断文件后缀是否合法
         if (!$this->checkAllowType($ext)) {
             throw new \Exception(BaseConst::UPLOAD_FILE_EXT_NOT_VALID_MSG, BaseConst::UPLOAD_FILE_EXT_NOT_VALID);
         }
     }
     // 传输文件
     $transer->setFilters($filters);
     $transer->receive($tmp);
     foreach ($files as $k => $v) {
         $fileInfo = $transer->getFileInfo($k);
         $data = array('name' => $fileInfo[$k]['name'], 'path' => $this->targetRel . $fileInfo[$k]['name'], 'type' => $fileInfo[$k]['type']);
         $ext = strstr($fileInfo[$k]['name'], '.');
         $tmpFileName = strstr($fileInfo[$k]['name'], '.', true);
         // 生成缩略图
         if ($this->ifThumb) {
             $thumbInfo = $this->makeThumb($this->targetAbs . $fileInfo[$k]['name'], $this->targetAbs . $tmpFileName . $this->thumbW . 'X' . $this->thumbH . $ext, $this->thumbW, $this->thumbH);
             $data['thumb'] = $thumbInfo;
         }
         // 加水印
         if ($this->ifWaterMark) {
             $this->makeWatermark($this->targetAbs . $fileInfo[$k]['name'], 6, '', $this->waterText, $_SERVER['DOCUMENT_ROOT'] . '/STXIHEI.TTF', $this->waterPct);
         }
         $result[] = $data;
     }
     if ($result) {
         return json_encode($result);
     } else {
         return false;
     }
 }