Beispiel #1
0
 public static function uploadFile($file, $content = null)
 {
     // 获取上传文件
     if (empty($file['name'])) {
         return false;
     }
     // 校验扩展名
     $part = explode('.', $file['name']);
     $ext = ($length = count($part)) > 1 ? strtolower($part[$length - 1]) : '';
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     // 获取插件配置
     $option = self::getConfig();
     $date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
     // 保存位置
     $savepath = preg_replace(array('/\\{year\\}/', '/\\{month\\}/', '/\\{day\\}/'), array($date->year, $date->month, $date->day), $option->savepath);
     $savename = $savepath . sprintf('%u', crc32(uniqid())) . '.' . $ext;
     if (isset($content)) {
         $savename = $content['attachment']->path;
         self::deleteFile($savename);
     }
     // 上传文件
     $filename = $file['tmp_name'];
     if (!isset($filename)) {
         return false;
     }
     // 初始化 SDK
     self::initSDK($option->accesskey, $option->sercetkey);
     // 上传凭证
     $policy = new Qiniu_RS_PutPolicy($option->bucket);
     $token = $policy->Token(null);
     $extra = new Qiniu_PutExtra();
     $extra->Crc32 = 1;
     // 上传
     list($result, $error) = Qiniu_PutFile($token, $savename, $filename, $extra);
     if ($error == null) {
         return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename));
     } else {
         return false;
     }
 }
Beispiel #2
0
 /**
  * 上传头像
  * @param array $file
  * @return boolean|multitype:string unknown number Ambigous <string, unknown> Ambigous <Ambigous, string, mixed>
  */
 private function uploadAvatar($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $ext = $this->getSafeName($file['name']);
     if (!Widget_Upload::checkFileType(strtolower($ext)) || Typecho_Common::isAppEngine()) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $path = Widget_Common::getAvatarPath($this->user->uid);
     $realPath = Typecho_Common::url($path, defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__);
     //创建上传目录
     if (!is_dir($realPath)) {
         if (!$this->makeAvatarDir($realPath)) {
             return false;
         }
     }
     //获取文件名
     $fileName = $this->user->uid . '.tmp.' . $ext;
     $realPath = $realPath . '/' . $fileName;
     if (isset($file['tmp_name'])) {
         //移动上传文件
         if (!@move_uploaded_file($file['tmp_name'], $realPath)) {
             return false;
         }
     } else {
         if (isset($file['bytes'])) {
             //直接写入文件
             if (!file_put_contents($realPath, $file['bytes'])) {
                 return false;
             }
         } else {
             return false;
         }
     }
     if (!isset($file['size'])) {
         $file['size'] = filesize($realPath);
     }
     //返回相对存储路径
     return array('name' => $file['name'], 'path' => $path, 'file' => $path . $fileName, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($realPath));
 }
Beispiel #3
0
 public static function uploadFile($file, $content = null)
 {
     // 获取上传文件
     if (empty($file['name'])) {
         return false;
     }
     $option = self::getConfig();
     if (!isset($option->accessid)) {
         return false;
     }
     self::initSDK();
     $obj = new ALIOSS($option->accessid, $option->accesskey, $option->endpoint);
     // 校验扩展名
     $part = explode('.', $file['name']);
     $ext = ($length = count($part)) > 1 ? strtolower($part[$length - 1]) : '';
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     // 保存位置
     $savename = str_replace(array('{year}', '{month}', '{day}'), array(date('Y'), date('m'), date('d')), ltrim(self::isImage($ext) ? $option->savepath : $option->nonimg_savepath, '/')) . sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $response = $obj->upload_file_by_file($option->bucket, $savename, $file['tmp_name']);
     if ($response->status === 200) {
         return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename));
     }
     return false;
 }
Beispiel #4
0
 /**
  * 上传文件处理函数
  *
  * @access public
  * @param array $file 上传的文件
  * @return mixed
  */
 public static function uploadHandle($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $fileName = preg_split("(\\/|\\|:)", $file['name']);
     $file['name'] = array_pop($fileName);
     //获取扩展名
     $ext = '';
     $part = explode('.', $file['name']);
     if (($length = count($part)) > 1) {
         $ext = strtolower($part[$length - 1]);
     }
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $date = new Typecho_Date($options->gmtTime);
     // 构建路径
     $path = Widget_Upload::UPLOAD_DIR . '/' . $date->year . '/' . $date->month;
     // 获取文件名
     $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path = $path . '/' . $fileName;
     // 上传云存储
     $bos = self::bosInit();
     if (isset($file['tmp_name'])) {
         $bos->uploadFile($file['tmp_name'], $path);
     } else {
         if (isset($file['bits'])) {
             $bos->uploadFileWithData($file['bits'], $path);
         } else {
             return false;
         }
     }
     $meta = null;
     if (!isset($file['size'])) {
         $meta = $meta ?: $bos->getObjectMeta($path);
         $file['size'] = $meta->contentLength;
     }
     if (!isset($file['type'])) {
         $meta = $meta ?: $bos->getObjectMeta($path);
         $file['type'] = $meta->contentType;
     }
     // 返回相对存储路径
     return ['name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => $file['type']];
 }
Beispiel #5
0
 /**
  * 上传文件处理函数
  *
  * @access public
  * @param array $file 上传的文件
  * @return mixed
  */
 public static function uploadHandle($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     //获取扩展名
     $ext = self::getSafeName($file['name']);
     if (!Widget_Upload::checkFileType($ext) || Typecho_Common::isAppEngine()) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $date = new Typecho_Date($options->gmtTime);
     //构建路径 /year/month/
     $path = '/' . $date->year . '/' . $date->month;
     $settings = $options->plugin('UpyunFile');
     if ($settings->mode == 'typecho') {
         $path = self::getUploadDir() . $path;
     }
     //获取文件名及文件路径
     $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path = $path . '/' . $fileName;
     $uploadfile = isset($file['tmp_name']) ? $file['tmp_name'] : (isset($file['bits']) ? $file['bits'] : FALSE);
     if ($uploadfile == FALSE) {
         return false;
     } else {
         //上传文件
         $upyun = self::upyunInit();
         $fh = fopen($uploadfile, 'rb');
         $upyun->writeFile($path, $fh, TRUE);
         fclose($fh);
     }
     if (!isset($file['size'])) {
         $fileInfo = $upyun->getFileInfo($path);
         $file['size'] = $fileInfo['x-upyun-file-size'];
     }
     //返回相对存储路径
     return array('name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path));
 }
Beispiel #6
0
 /**
  * 上传附件
  * 
  * @static
  * @access public
  * @return bool
  * @throws Typecho_Plugin_Exception
  */
 public static function ScsUpload($file, $content = null)
 {
     if (empty($file['name'])) {
         return false;
     }
     //获取安全的文件名后缀
     $ext = self::getSafeName($file['name']);
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     $option = self::getSCSconfig();
     $date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
     $path = $option->format == null ? $date->year . '/' . $date->month . '/' : preg_replace(array('/\\{year\\}/', '/\\{month\\}/', '/\\{day\\}/'), array($date->year, $date->month, $date->day), self::getSCSFilepath($option->format));
     /*非必须(在本地附件目录/usr/uploads/下创建新目录)
             if (!is_dir($path)) {
                 if (!self::makeUploadDir($path)) {
                     return false;
                 }
             }
     		*/
     //以Unix time stamp形式储存:$path .= sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path .= $file['name'];
     if (isset($content)) {
         $path = $content['attachment']->path;
         self::ScsDelete($path);
     }
     $clienttmp = $file['tmp_name'];
     if (!isset($clienttmp)) {
         return false;
     }
     self::getSCSsdk();
     $scs = new SCS($option->accesskey, $option->secretkey);
     if ($scs->putObjectFile($clienttmp, $option->bucket, $path, SCS::ACL_PUBLIC_READ)) {
         return array('name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path));
     } else {
         return false;
     }
 }
Beispiel #7
0
 /**
  * 上传文件处理函数
  *
  * @access public
  * @param array $file 上传的文件
  * @return mixed
  */
 public static function uploadHandle($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $fileName = preg_split("(\\/|\\|:)", $file['name']);
     $file['name'] = array_pop($fileName);
     //获取扩展名
     $ext = '';
     $part = explode('.', $file['name']);
     if (($length = count($part)) > 1) {
         $ext = strtolower($part[$length - 1]);
     }
     if (!Widget_Upload::checkFileType($ext)) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $date = new Typecho_Date($options->gmtTime);
     //构建路径
     $path = Widget_Upload::UPLOAD_PATH . '/' . $date->year . '/' . $date->month;
     //获取文件名
     $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
     $path = $path . '/' . $fileName;
     $bcs = self::bcsInit();
     $bucket = $options->plugin('BaeUpload')->bucket;
     //空日志记录函数
     function bcs_log()
     {
     }
     if (isset($file['tmp_name'])) {
         //移动上传文件
         if (!$bcs->create_object($bucket, $path, $file['tmp_name'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->isOK()) {
             return false;
         }
     } else {
         if (isset($file['bits'])) {
             //直接写入文件
             if (!$bcs->create_object_by_content($bucket, $path, $file['bits'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->isOK()) {
                 return false;
             }
         } else {
             return false;
         }
     }
     //设置文件Content-Type
     $bcs->set_object_meta($bucket, $path, array('Content-Type' => BCS_MimeTypes::get_mimetype($ext)), array(BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'));
     if (!isset($file['size'])) {
         $file['size'] = $bcs->get_object_info($bucket, $path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->header['Content-Length'];
     }
     //返回相对存储路径
     return array('name' => $file['name'], 'path' => $path, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($path));
 }