Пример #1
0
 /**
  * 上传文件
  * @access public
  * @param string $savePath  上传文件保存路径
  * @return string
  * @throws ThinkExecption
  */
 public function upload($savePath = '')
 {
     if (!$this->isOpen()) {
         $this->error = '没有开启云图片功能';
         return false;
     }
     $fileInfo = array();
     $isUpload = false;
     // 获取上传的文件信息,对$_FILES数组信息处理
     $files = $this->dealFiles($_FILES);
     foreach ($files as $key => $file) {
         //过滤无效的上传
         if (!empty($file['name'])) {
             $file['key'] = $key;
             $file['extension'] = $this->getExt($file['name']);
             if ($this->savePath) {
                 $file['savepath'] = $this->savePath;
             } else {
                 $file['savepath'] = $this->customPath;
             }
             if ($this->saveName) {
                 $file['savename'] = $this->saveName;
             } else {
                 $file['savename'] = uniqid() . "." . $file['extension'];
             }
             //移动设备上传的无后缀的图片,默认为jpg
             if ($GLOBALS['fromMobile'] == true && empty($file['extension'])) {
                 $file['extension'] = 'jpg';
                 $file['savename'] = trim($file['savename'], '.') . '.jpg';
             } elseif ($this->autoCheck) {
                 if (!$this->check($file)) {
                     return false;
                 }
             }
             //计算hash
             if (function_exists($this->hashType)) {
                 $fun = $this->hashType;
                 $file['hash'] = $fun($file['tmp_name']);
             }
             //上传到云服务器
             $config = $this->getConfig();
             tsload(ADDON_PATH . '/library/upyun.class.php');
             $cloud = new UpYun($config['cloud_image_bucket'], $config['cloud_image_admin'], $config['cloud_image_password']);
             $cloud->setTimeout(60);
             $file_content = file_get_contents($file['tmp_name']);
             $res = $cloud->writeFile('/' . $file['savepath'] . $file['savename'], $file_content, true);
             if (!$res) {
                 $this->error = '上传到云服务器失败!';
                 return false;
             }
             //上传成功后保存文件信息,供其它地方调用
             unset($file['tmp_name'], $file['error'], $file_content);
             $fileInfo[] = $file;
             $isUpload = true;
         }
     }
     if ($isUpload) {
         $this->uploadFileInfo = $fileInfo;
         return true;
     } else {
         $this->error = '上传出错!文件不符合上传要求。';
         return false;
     }
 }