Beispiel #1
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 = Forum_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 . '.' . $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));
 }